SRL Archive - Help and Support

Combining String Arrays


Narcle

2008-10-26 04:37:30

Edit: Or not…

Okay… I've tried couple different ways and this is what I have atm:
Function CombineStrArr(Ar1, Ar2: TStringArray): TStringArray;
var
L, H, i: integer;
begin
SetArrayLength(Result, Length(Ar1) + Length(Ar2));
L := Low(Ar1);
H := High(Ar1);
For i := L to H do
Result[i] := Ar1[i];//Line with error (174)
L := H + 1;
H := High(Result);
for i := L to H do
Result[i] := Ar2[i-L];
end;
And I get this error:
[Runtime Error] : Exception: Access violation at address 00405266 in module 'scar.exe'. Read of address FFFFFFF7 in line 174 in script


I originally had Result := Ar1; but then Widget pointed out to me that it would make it a pointer so I couldn't do that. The code above I use the same as for CombineIntArray and works; I know Wizzup has one but I wanted to make sure they were in order so I made one.

Any help would be much appreciated.

Janilabo

2008-10-26 04:41:30

function TSAArrayToTSA(TSAArray: array of TStringArray): TStringArray;
var
i, i2, i3, H1, H2, tH: Integer;
begin
H1:= High(TSAArray);
for i:= 0 to H1 do
IncEx(tH, High(TSAArray[i]) + 1);
SetArrayLength(Result, tH);
for i:= 0 to H1 do
begin
H2:= High(TSAArray[i]);
for i2:= 0 to H2 do
begin
Result[i3]:= TSAArray[i][i2];
Inc(i3);
end;
end;
end;

There you go

Daniel

2008-10-26 04:46:23

Could something like this help?:
function JoinTSA(Ar1, Ar2: TStringArray): TStringArray;
var
i, ArL: Integer;
begin
Result:=Ar1;
SetLength(Result, Length(Ar1)+Length(Ar2));
ArL:=Length(Ar2);
for i:=0 to High(Ar2) do
Result[i+ArL]:=Ar2[i];
end;

EDIT: Damn, beat me to it Jani :p

Narcle

2008-10-26 04:51:03

Nope same error still.

Janilabo

2008-10-26 04:52:34

function TSAArrayToTSA(TSAArray: array of TStringArray): TStringArray;
var
i, i2, i3, H1, H2, tH: Integer;
begin
H1:= High(TSAArray);
for i:= 0 to H1 do
IncEx(tH, High(TSAArray[i]) + 1);
SetArrayLength(Result, tH);
for i:= 0 to H1 do
begin
H2:= High(TSAArray[i]);
for i2:= 0 to H2 do
begin
Result[i3]:= TSAArray[i][i2];
Inc(i3);
end;
end;
end;

var
tmp: array[0..2] of TStringArray;
TSA: TStringArray;
i: Integer;

begin
tmp[0]:= ['1', '2', '3'];
tmp[1]:= ['4', '5', '6', '7']
tmp[2]:= ['8', '9'];
TSA:= TSAArrayToTSA([tmp[0], tmp[1], tmp[2]]);
for i:= 0 to High(TSA) do
WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
end.

Test that.. :)

Daniel

2008-10-26 04:54:48

Janilabo, it's not an Array of TStringArray ;) Just a TStringArray ;)

EDIT: But apparently Narcle had just figured out what was wrong. She said she was calling it incorrectly.

Narcle

2008-10-26 04:56:32

Finally, I figured it out. The way I was using it, I had to put a variable into a temp array otherwise I get that… Real stupid.

Thx for trying though guys. :D

(btw mine works /yay)

Janilabo

2008-10-26 04:58:57

Well Dan, isn't that just extra? :D You could use mine with 2 TStringArrays too, my example works with 3, though.

function TSAArrayToTSA(TSAArray: array of TStringArray): TStringArray;
var
i, i2, i3, H1, H2, tH: Integer;
begin
H1:= High(TSAArray);
for i:= 0 to H1 do
IncEx(tH, High(TSAArray[i]) + 1);
SetArrayLength(Result, tH);
for i:= 0 to H1 do
begin
H2:= High(TSAArray[i]);
for i2:= 0 to H2 do
begin
Result[i3]:= TSAArray[i][i2];
Inc(i3);
end;
end;
end;

var
ar1, ar2, TSA: TStringArray;
i: Integer;

begin
ar1:= ['1', '2', '3', '4', '5'];
ar2:= ['6', '7', '8', '9']
TSA:= TSAArrayToTSA([ar1, ar2]);
for i:= 0 to High(TSA) do
WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
end.

..as you see… :p Does the job..

Daniel

2008-10-26 05:02:15

Yes, i know. But that maybe what Narcle wasn't trying to do.

Narcle

2008-10-26 05:27:28

Or not… Now I'm getting errors again. WTF

This is how i'm declaring it :
for i := 0 to HowManyPlayers - 1 do

if Echo[i].PickOptions[1] then//Charm Pick-ups
begin
Echo[i].ObjList := CombineIntArr(Echo[i].ObjList, CharmIDs);
Temp := Echo[i].ObjOption;
Echo[i].ObjOption := CombineStrArr(Temp, CharmNames);
end;

Wizzup?

2008-10-26 05:53:45

EDIT:

It just works, Narcle… :)

program New;

Function CombineStrArr(Ar1, Ar2: TStringArray): TStringArray;
var
L, H, i: integer;
begin
SetArrayLength(Result, Length(Ar1) + Length(Ar2));
L := Low(Ar1);
H := High(Ar1);
For i := L to H do
Result[i] := Ar1[i];//Line with error (174)
L := H + 1;
H := High(Result);
for i := L to H do
Result[i] := Ar2[i-L];
end;
var
ar1, ar2, TSA: TStringArray;
i: integer;

begin
ar1:= ['1', '2', '3', '4', '5'];
ar2:= ['6', '7', '8', '9']
TSA:= CombineStrArr(ar1, ar2);
for i:= 0 to High(TSA) do
WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
end.

Narcle

2008-10-26 06:05:42

Then it must be the Global variable I'm working with. It will NOT combine… sigh. But it combines Integer arrays just fine.

R0b0t1

2008-10-26 16:43:56

What? You just make an array that's maximum length is High(arr1) + High(arr2).

Lee Lok Hin

2008-10-27 05:28:15

program New;

function CombineSArray(t,s:TStringArray):TStringArray;
var
i,ts,p:integer;
begin
SetArrayLength(Result, Length(t) + Length(s));
ts:= high(t);
for i:=0 to ts do
Result[i] := t[i];
p := high(s);
for i:= 0 to p do
begin
Result[ts + 1]:= s[i]
ts:= ts + 1;
end;
end;


procedure DelBlankAll(var s:TStringArray);
var
i,t,p,highs,x,qe:integer;
ss,ts:TStringArray;

begin
repeat
writeln('Repeat');
qe:= 0;
highs:= length(s);
writeln(inttostr(highs));
for i:=0 to highs do
begin
writeln('I is: '+inttostr(i));
if i = Length(s) then Exit;
if s[i] = '' then break;
end;
SetArrayLength(ss, i);
for p:= 0 to i - 1 do
ss[p]:= s[p];
SetArrayLength(ts,highs - i - 1);
for p:= i + 1 to highs - 1do
begin
ts[qe]:= s[p];
inc(qe);
end;
s:= CombineSArray(ss,ts);
for x:= 0 to high(s) do
writeln(s[x]);
until(false)
end;

var
y:integer;
Black,sy,sty:TSTringArray;

begin
cleardebug;
Black:= ['p','t','y','e','','','d',''];
DelBlankAll(Black);

end.

How about this? I've used this for my IRC bot's BList, and it should work fine.