Inno Setup 从存储在数组中的逗号分隔字符串创建数组
Inno Setup create array from comma delimited strings stored in an array
我需要用一些逗号分隔的字符串创建一个数组,这些字符串本身存储在一个数组中。包含逗号分隔字符串列表的主数组是 RemoteSiteLines
并且可以包含任意数量的字符串条目,例如 1001,Remote Site 1,REM1,0
、1002,Remote Site 2,REM2,1
等,我需要进入另一个数组 RemoteSiteDetailsLines
.
我什至不知道从哪里开始,除非我弄错了,否则在 Inno Setup 中没有内置函数可以做这样的事情。任何人都可以建议执行此操作的程序吗?
使用像array of array of string
这样的二维数组。
解析可以像下面这样完成。它不是最高效的代码,但它相对简单和简短。
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;
结果会是这样的:
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
我需要用一些逗号分隔的字符串创建一个数组,这些字符串本身存储在一个数组中。包含逗号分隔字符串列表的主数组是 RemoteSiteLines
并且可以包含任意数量的字符串条目,例如 1001,Remote Site 1,REM1,0
、1002,Remote Site 2,REM2,1
等,我需要进入另一个数组 RemoteSiteDetailsLines
.
我什至不知道从哪里开始,除非我弄错了,否则在 Inno Setup 中没有内置函数可以做这样的事情。任何人都可以建议执行此操作的程序吗?
使用像array of array of string
这样的二维数组。
解析可以像下面这样完成。它不是最高效的代码,但它相对简单和简短。
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;
结果会是这样的:
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