SRL Archive - OSR Help

Splitting an array


Bobzilla69

2011-12-03 04:02:17

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

Sex

2011-12-03 04:21:28

You should be able to figure it out with this :).
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.

Bobzilla69

2011-12-03 04:22:49

ok

edit:

Any idea why i get error from this:
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

Sex

2011-12-03 04:28:55

 for i := 1 to Length(ArrayWithData) do
Change it to:
 i:= 0 to high(ArrayWithData)

Bobzilla69

2011-12-03 04:30:45

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

Sex

2011-12-03 04:33:53

Look at this:
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:
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.

Bobzilla69

2011-12-03 04:38:17

From: Sex
Look at this:
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:
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 :)

Killerdou

2011-12-03 08:00:15


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.