Reputation: 1957
I need to create an array from a number of comma delimited strings which are stored in an array themselves. The master array which contains the list of comma delimited strings is RemoteSiteLines
and can contain any number of string entries like 1001,Remote Site 1,REM1,0
, 1002,Remote Site 2,REM2,1
etc. which I need to get into another array RemoteSiteDetailsLines
.
I am struggling to know even where to start with this and unless I am mistaken there are no built-in functions to do something like this in Inno Setup. Can anyone suggest a procedure for doing this?
Upvotes: 1
Views: 1837
Reputation: 202474
Use a two-dimensional array like array of array of string
.
Parsing can be done like below. It's not the most efficient code, but it's relatively simple and short.
procedure ParseArray(Lines: array of string; var Tokens: array of array of string);
var
Count, Index, Index2, P: Integer;
Line: string;
begin
Count := GetArrayLength(Lines);
{ Allocate target array }
SetArrayLength(Tokens, Count);
{ Iterate lines }
for Index := 0 to Count - 1 do
begin
Line := Lines[Index];
Log(Format('Line[%d]: %s', [Index, Line]));
Index2 := 0;
{ Loop until we consume whole line }
while Line <> '' do
begin
{ Look for the next delimiter }
P := Pos(',', Line);
{ Reallocate array to fit yet another token }
SetArrayLength(Tokens[Index], Index2 + 1);
if P > 0 then
begin
{ Copy the token to the target array }
Tokens[Index][Index2] := Copy(Line, 1, P - 1);
{ Remove the token and the delimiter from the string, }
{ so that we can look for the next token in the next iteration }
Delete(Line, 1, P);
end
else
begin
Tokens[Index][Index2] := Line;
{ Got last token, break the loop }
Line := '';
end;
Log(Format('Token[%d][%d]: %s', [Index, Index2, Tokens[Index][Index2]]));
Inc(Index2);
end;
end;
end;
{ Usage example }
procedure InitializeWizard();
var
RemoteSiteLines: array of string;
RemoteSiteDetailsLines: array of array of string;
begin
SetArrayLength(RemoteSiteLines, 3);
RemoteSiteLines[0] := '1001,Remote Site 1,REM1,0';
RemoteSiteLines[1] := '1002,Remote Site 2,REM2,0';
RemoteSiteLines[2] := '1003,Remote Site 3,REM3,0';
ParseArray(RemoteSiteLines, RemoteSiteDetailsLines);
end;
The result will be like:
Line[0]: 1001,Remote Site 1,REM1,0
Token[0][0]: 1001
Token[0][1]: Remote Site 1
Token[0][2]: REM1
Token[0][3]: 0
Line[1]: 1002,Remote Site 2,REM2,0
Token[1][0]: 1002
Token[1][1]: Remote Site 2
Token[1][2]: REM2
Token[1][3]: 0
Line[2]: 1003,Remote Site 3,REM3,0
Token[2][0]: 1003
Token[2][1]: Remote Site 3
Token[2][2]: REM3
Token[2][3]: 0
Upvotes: 3