Results 1 to 8 of 8

Thread: Splitting an array

  1. #1
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Splitting an array

    Hi,

    Hope someone can refresh my memory because i am stuck.

    I want to take an array (TVariantArray) and i want to split it into two arrays, but in a specific way.

    I want to have the first result [0] to go into A:TStringArray but the [1] to go into B:TStringArray, then [2] into A, then [3] into B.
    so really have it alternate between the two arrays for every result.

    Thanks for the help

  2. #2
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    You should be able to figure it out with this .
    Simba Code:
    program new;
    var
      TVA : TVariantArray;
      i, h : integer;
    begin
      TVA := [5, 4, 'three', 'two',  true];
      h := high(TVA);
      for i := 0 to h do
      begin
        if i mod 2 = 0 then
          Writeln('First array: ' + ToStr(TVA[i]))
        else
          Writeln('Second array: ' + ToStr(TVA[i]));
      end;
    end.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  3. #3
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    ok

    edit:

    Any idea why i get error from this:
    Simba Code:
    SetLength(PRS_SV, Length(ArrayWithData));
      SetLength(PRS_SD, Length(ArrayWithData));
      for i := 1 to Length(ArrayWithData) do
      begin
        if i mod 2 = 0 then
        begin
          PRS_SV[i] := ArrayWithData[i];
        end else
        begin
          PRS_SD[i] := ArrayWithData[i]; ///Error: Out Of Range at line 128
        end;
      end;
    i skip first entry in array on purpose
    Last edited by Bobzilla69; 12-03-2011 at 09:28 AM.

  4. #4
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Simba Code:
    for i := 1 to Length(ArrayWithData) do
    Change it to:
    Simba Code:
    i:= 0 to high(ArrayWithData)
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  5. #5
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Same error, and i need to skip first entry in the array

  6. #6
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Look at this:
    Simba Code:
    program new;
    var
      TVA, TVA1, TVA2 : TVariantArray;

    procedure SplitArr(Arr : TVariantArray; out Res1, Res2 : TVariantArray);
    var
      h, i : integer;
    begin
      h := High(Arr);
      for 0 := 1 to h do
      begin
        if i mod 2 = 0 then
        begin
          SetArrayLength(Res1, GetArrayLength(Res1) + 1);
          Res1[high(Res1)] := Arr[i];
        end else
        begin
          SetArrayLength(Res2, GetArrayLength(Res2) + 1);
          Res2[high(Res2)] := Arr[i];
        end;
      end;
    end;
    begin
      TVA := [5, 4, 'three', 'two',  true];
      SplitArr(TVA, TVA1, TVA2);
      Writeln('Set 1: ' + ToStr(TVA1));
      Writeln('Set 2: ' + ToSTr(TVA2));
    end.
    If you skip the first entry you're going to have to switch the arrays because the 1 is odd.

    Edit: ^ like this:
    Simba Code:
    program new;
    var
      TVA, TVA1, TVA2 : TVariantArray;

    procedure SplitArr(Arr : TVariantArray; out Res1, Res2 : TVariantArray);
    var
      h, i : integer;
    begin
      h := High(Arr);
      for i := 1 to h do
      begin
        if i mod 2 = 0 then
        begin
          SetArrayLength(Res2, GetArrayLength(Res2) + 1);
          Res2[high(Res2)] := Arr[i];
        end else
        begin
          SetArrayLength(Res1, GetArrayLength(Res1) + 1);
          Res1[high(Res1)] := Arr[i];
        end;
      end;
    end;
    begin
      TVA := [5, 4, 'three', 'two',  true];
      SplitArr(TVA, TVA1, TVA2);
      Writeln('Set 1: ' + ToStr(TVA1));
      Writeln('Set 2: ' + ToSTr(TVA2));
    end.
    Last edited by Sex; 12-03-2011 at 09:37 AM.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  7. #7
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Sex View Post
    Look at this:
    Simba Code:
    program new;
    var
      TVA, TVA1, TVA2 : TVariantArray;

    procedure SplitArr(Arr : TVariantArray; out Res1, Res2 : TVariantArray);
    var
      h, i : integer;
    begin
      h := High(Arr);
      for i := 0 to h do
      begin
        if i mod 2 = 0 then
        begin
          SetArrayLength(Res1, GetArrayLength(Res1) + 1);
          Res1[high(Res1)] := Arr[i];
        end else
        begin
          SetArrayLength(Res2, GetArrayLength(Res2) + 1);
          Res2[high(Res2)] := Arr[i];
        end;
      end;
    end;
    begin
      TVA := [5, 4, 'three', 'two',  true];
      SplitArr(TVA, TVA1, TVA2);
      Writeln('Even: ' + ToStr(TVA1));
      Writeln('Odd: ' + ToSTr(TVA2));
    end.
      h, i : integer;
    begin
      h := High(Arr);
      for i := 0 to h do
      begin
        if i mod 2 = 0 then
        begin
          SetArrayLength(Res1, GetArrayLength(Res1) + 1);
          Res1[high(Res1)] := Arr[i];
        end else
        begin
          SetArrayLength(Res2, GetArrayLength(Res2) + 1);
          Res2[high(Res2)] := Arr[i];
        end;
      end;
    end;
    begin
      TVA := [5, 4, 'three', 'two',  true];
      SplitArr(TVA, TVA1, TVA2);
      Writeln(TVA1);
      Writeln(TVA2);
    end.
    If you skip the first entry you're going to have to switch the arrays because the 1 is odd.

    Edit: ^ like this:
    Simba Code:
    program new;
    var
      TVA, TVA1, TVA2 : TVariantArray;

    procedure SplitArr(Arr : TVariantArray; out Res1, Res2 : TVariantArray);
    var
      h, i : integer;
    begin
      h := High(Arr);
      for i := 1 to h do
      begin
        if i mod 2 = 0 then
        begin
          SetArrayLength(Res2, GetArrayLength(Res2) + 1);
          Res2[high(Res2)] := Arr[i];
        end else
        begin
          SetArrayLength(Res1, GetArrayLength(Res1) + 1);
          Res1[high(Res1)] := Arr[i];
        end;
      end;
    end;
    begin
      TVA := [5, 4, 'three', 'two',  true];
      SplitArr(TVA, TVA1, TVA2);
      Writeln('Set 1: ' + ToStr(TVA1));
      Writeln('Set 2: ' + ToSTr(TVA2));
    end.
    WOW, kinda confused me there but it did help thanks, i noticed i tried to access one number to many in the array which is why it wouldnt work.

    But thanks for all the help

  8. #8
    Join Date
    Nov 2006
    Posts
    1,103
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    Simba Code:
    program new;
    var
      TVA, TVA1, TVA2 : TVariantArray;

    procedure OtherSplitArr(Arr : TVariantArray; out Res1, Res2 : TVariantArray);
    var
      h, i : integer;
    begin
      h := High(Arr);
      for i := 0 to h do
      begin
        if i mod 2 = 0 then
        begin
          SetArrayLength(Res1, GetArrayLength(Res1) + 1);
          Res1[high(Res1)] := Arr[i];
        end else
        begin
          SetArrayLength(Res2, GetArrayLength(Res2) + 1);
          Res2[high(Res2)] := Arr[i];
        end;
      end;
    end;

    procedure KillerSplitArr(Arr : TVariantArray; out Res1, Res2 : TVariantArray);
    var
      h,h1,h2, i : integer;
    begin
      h := Length(Arr);
      h2 := Floor(h/2);
      h1 := h-h2;
      SetArrayLength(Res1, h1 );
      SetArrayLength(Res2, h2 );
      for i := 0 to h2-1 do
      begin

        Res1[i] := Arr[i*2];
        Res2[i] := Arr[i*2+1];
      end;
      if not (h1=h2) then Res1[h1-1]:= Arr[h-1];
    end;


    var
    time,killertime, othertime, i, i2: integer;


    begin
      TVA := [5, 4, 'three', 'two',  true];

      for i2 := 0 to 100 do
        begin
          time := getsystemtime;
          for i := 0 to 1000 do
            KillerSplitArr(TVA, TVA1, TVA2);
          killertime := killertime + getsystemtime-time;
          time := getsystemtime;
          for i := 0 to 1000 do
             OtherSplitArr(TVA, TVA1, TVA2);
           othertime := othertime + getsystemtime-time;
        end;
       writeln(killertime);
       writeln(othertime);
    end.
    Infractions, reputation, reflection, the dark side of scripting, they are.

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
  •