Results 1 to 6 of 6

Thread: string <-> T3DIntArray

  1. #1
    Join Date
    Aug 2008
    Location
    Finland
    Posts
    2,851
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default string <-> T3DIntArray

    Simba Code:
    program New;

    function T3DIntArrToString(Arr: T3DIntegerArray): string;
    var
      i, ii, iii, H, HH, HHH: integer;
    begin
      H := High(Arr);
      for i := 0 to H do
      begin
        Result := Result + '(';
        HH := High(Arr[i]);
        for ii := 0 to HH do
        begin
          Result := Result + '[';
          HHH := High(Arr[i][ii]);
          for iii := 0 to HHH do
            Result := Result + IntToStr(Arr[i][ii][iii])+'#';
          Result := Result + ']';
        end;
        Result := Result + ')';
      end;
    end;


    function StringToIntArr(s: string): TIntegerArray;
    var
      a, L, LL: integer;
      tmp: string;
    begin
      L := Length(s);
      for a := 1 to L do
      begin
        if s[a] <> '#' then
          tmp := tmp + s[a]
        else
        begin
          LL := Length(Result);
          SetLength(Result, LL + 1);
          Result[LL] := StrToInt(GetNumbers(tmp));
          tmp := '';
        end;
      end;
    end;

    function StringTo2DIntArr(s: string): T2DIntegerArray;
    var
      a, L, LS, LE: integer;
      StartBrackets, EndBrackets: TIntegerArray;
      tmp: string;
    begin
      L := Length(s);
      for a := 1 to L do
        if s[a] = '[' then
        begin
          LS := Length(StartBrackets);
          SetLength(StartBrackets, LS + 1);
          StartBrackets[LS] := a;
        end else if s[a] = ']' then
        begin
          LE := Length(EndBrackets);
          SetLength(EndBrackets, LE + 1);
          EndBrackets[LE] := a;
        end;

      SetLength(Result, Length(StartBrackets));
      L := High(Result);
      for a := 0 to L do
        Result[a] := StringToIntArr(Copy(s, StartBrackets[a], EndBrackets[a]-StartBrackets[a]));
    end;


    function iCopy(s: string; iStart, iEnd: integer): string;
    var
      i: integer;
    begin
      for i := iStart to iEnd do
        Result := Result + s[i];
    end;

    function StringTo3DIntArr(s: string): T3DIntegerArray;
    var
      a, L, LS, LE: integer;
      StartBrackets, EndBrackets: TIntegerArray;
      tmp: string;
    begin
      L := Length(s);
      for a := 1 to L do
        if s[a] = '(' then
        begin
          LS := Length(StartBrackets);
          SetLength(StartBrackets, LS + 1);
          StartBrackets[LS] := a;
        end else if s[a] = ')' then
        begin
          LE := Length(EndBrackets);
          SetLength(EndBrackets, LE + 1);
          EndBrackets[LE] := a;
        end;

      SetLength(Result, Length(StartBrackets));
      L := High(Result);
      for a := 0 to L do
        Result[a] := StringTo2DIntArr(iCopy(s, StartBrackets[a], EndBrackets[a]));
    end;

    var
      tia: t3dintegerarray;
      s: string;

    begin
      SetLength(tia, RandomRange(3, 5));
      for i := 0 to high(tia) do
      begin
        SetLength(tia[i], RandomRange(3, 5));
        for ii := 0 to high(tia[i]) do
        begin
          SetLength(tia[i][ii], RandomRange(3, 5));
          for iii := 0 to high(tia[i][ii]) do
            tia[i][ii][iii] := RandomRange(0, 9);
        end;
      end;
      s := t3dintarrtostring(tia);
      writeln(s);
      tia := StringTo3DIntArr(s);
      s := t3dintarrtostring(tia);
      writeln(s);
    end.

    I want to turn a 3d integer array into a string and vice versa, but the StringTo3DIntArr seems to mess up something.

    Above I first create a 3d int array, then turn it into a string and print it. That's just fine. But when I try to turn it back to a T3DIntegerArray and to a string again, the output is not the same anymore.

    Simba Code:
    ([4#3#4#][2#5#0#][4#8#6#][4#8#5#1#])([7#3#8#1#][5#7#3#8#][1#1#1#1#])([0#2#3#][2#1#0#][5#1#4#][8#6#8#2#])([6#7#0#3#][6#2#5#][1#6#4#][1#2#4#])

    ([4#3#4#][2#5#0#][4#8#6#][4#8#5#1#])([7#3#8#1#][5#7#3#8#][1#1#1#1#])([0#2#3#][2#1#0#][5#1#4#][8#6#8#2#])([6#7#0#3#][6#2#5#][1#6#4#][1#2#4#])([4#3#4#][2#5#0#][4#8#6#][4#8#5#1#])([7#3#8#1#][5#7#3#8#][1#1#1#1#])([0#2#3#][2#1#0#][5#1#4#][8#6#8#2#])([6#7#0#3#][6#2#5#][1#6#4#][1#2#4#])

    As you can see, somewhere the array is doubled.


    Where? I can't find it!!

  2. #2
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Weird.... I just wrote up my own and Im also having almost the same problem.

    E: Found my mistake. Im gonna check yours too see if you also made it.
    E2: Hmm I see your using SCAR, Mine wont work for you because it uses Simba's built in ToStr function to do the
    T3DIntArr->string conversion (Altho you could write up your own if you would like to use mine?) Still looking thru yours.
    E3: If narrowed it down to "StringToIntArr"
    E4: Wow simple..... and it stumped me.. add "SetLength(Result, 0);" to the begining of "StringToIntArr".
    also If you want this to be Simba compatible
    Simba Code:
    Result[LL] := StrToInt({$IFDEF SIMBA}ExtractFromStr{$ELSE}GetNumbers{$ENDIF}(tmp{$IFDEF SIMBA}, Numbers{$ENDIF}));
    E5: Removed Script posted new reply.

    The one I made - Removed. Look Below v
    Last edited by Dgby714; 01-09-2011 at 02:03 PM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  3. #3
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Can't you just use ToStr() ?
    Simba Code:
    var
      Arr : array of array of TIntegerArray;

    begin
      SetLength(Arr, 1);
      SetLength(Arr[0], 2);
      Arr[0][0] := [1, 2, 3];
      Arr[0][1] := [4, 5, 6, 7];
      WriteLn(ToStr(Arr));
    end.


    E: Sorry.. My bad >_< I didn't read the thread properly...

  4. #4
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    My Script - (Works in Simba and SCAR) The generated strings are also program independent so if you saving them to a file they can be used in both Simba and SCAR.

    Simba Code:
    program new;
    type
      T3DIntegerArray = array of T2DIntegerArray;

    function StrToT3DIA(Str: string): T3DIntegerArray;
    var
      I, J, K, L, TSL: integer;
      L2DB, R2DB, LB, RB: TIntegerArray;
      TempStr, TempStr2: string;
      StrArr: TStringArray;
    begin
      L := Length(Str);
      if (L < 1) then
        Exit;

      if (not ((Str[1] = '[') and (Str[L] = ']'))) then
        Exit;

      for I := 2 to L - 2 do
      begin
        if (Str[I] + Str[I + 1] = '[[') then
        begin
          SetLength(L2DB, Length(L2DB) + 1);
          L2DB[High(L2DB)] := I + 1;
        end;

        if (Str[I] + Str[I + 1] = ']]') then
        begin
          SetLength(R2DB, Length(R2DB) + 1);
          R2DB[High(R2DB)] := I + 1;
        end;
      end;
      if (not (Length(L2DB) = Length(R2DB))) then
        Exit;

      SetLength(Result, Length(L2DB));
      for I := 0 to High(L2DB) do
      begin
        TempStr := Copy(Str, L2DB[I], R2DB[I] - L2DB[I]);
        TSL := Length(TempStr);
        SetLength(LB, 0);
        SetLength(RB, 0);
        for J := 1 to TSL do
        begin
          if (TempStr[J] = '[') then
          begin
            SetLength(LB, Length(LB) + 1);
            LB[High(LB)] := J + 1;
          end;

          if (TempStr[J] = ']') then
          begin
            SetLength(RB, Length(RB) + 1);
            RB[High(RB)] := J;
          end;
        end;
        if (not (Length(LB) = Length(RB))) then
          Exit;

        SetLength(Result[I], Length(LB));
        for J := 0 to High(LB) do
        begin
          TempStr2 := Copy(TempStr, LB[J], RB[J] - LB[J]);
          StrArr := Explode(', ', TempStr2);
          SetLength(Result[I][J], Length(StrArr));
          for K := 0 to High(StrArr) do
            Result[I][J][K] := StrToIntDef(StrArr[K], 0);
        end;
      end;
    end;

    function T3DIAToStr(T3DIA: T3DIntegerArray): string;
    {$IFDEF SIMBA}
    begin
      Result := ToStr(T3DIA);
    end;
    {$ELSE}
    var
      I, J, K: integer;
    begin
      Result := '[';
      for I := 0 to High(T3DIA) do
      begin
        Result := Result + '[';
        for J := 0 to High(T3DIA[I]) do
        begin
          Result := Result + '[';
          for K := 0 to High(T3DIA[I][J]) do
          begin
            Result := Result + IntToStr(T3DIA[I][J][K]);
            if (not (K >= High(T3DIA[I][J]))) then
              Result := Result + ', ';
          end;
          Result := Result + ']';
          if (not (J >= High(T3DIA[I]))) then
            Result := Result + ', ';
        end;
        Result := Result + ']';
        if (not (I >= High(T3DIA))) then
          Result := Result + ', ';
      end;
      Result := Result + ']';
    end;
    {$ENDIF}

    var
      T: T3DIntegerArray;
      I, J, K: integer;
      S: string;
    begin
      SetLength(T, RandomRange(3, 6));
      for I := 0 to High(T) do
      begin
        SetLength(T[I], RandomRange(3, 6));
        for J := 0 to High(T[I]) do
        begin
          SetLength(T[I][J], RandomRange(3, 6));
          for K := 0 to High(T[I][J]) do
            T[I][J][K] := Random(100);
        end;
      end;

      S := T3DIAToStr(T);
      WriteLn(S);
      T := StrToT3DIA(S);
      WriteLn(T3DIAToStr(T));
    end.
    Last edited by Dgby714; 01-09-2011 at 02:09 PM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  5. #5
    Join Date
    Aug 2008
    Location
    Finland
    Posts
    2,851
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

    Thanks a lot Dgby! I will use that in my walker, you'll find yourself in the credits!

  6. #6
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Nice Dgby

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •