Inno Setup根据存储在数组中的逗号分隔的字符串创建数组 [英] Inno Setup create array from comma delimited strings stored in an array

查看:259
本文介绍了Inno Setup根据存储在数组中的逗号分隔的字符串创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用多个逗号分隔的字符串创建一个数组,这些字符串本身存储在数组中.包含逗号分隔字符串列表的主数组是RemoteSiteLines,并且可以包含任意数量的字符串条目,例如1001,Remote Site 1,REM1,01002,Remote Site 2,REM2,1等.我需要进入另一个数组RemoteSiteDetailsLines.

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.

即使是从哪里开始,我也很难知道,除非我弄错了,否则在Inno Setup中没有内置函数可以执行类似的操作.有人可以建议这样做的程序吗?

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?

推荐答案

使用像array of array of string这样的二维数组.

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;

结果将是:

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

这篇关于Inno Setup根据存储在数组中的逗号分隔的字符串创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆