SRL Archive - Help and Support

Adding arrays together?


Narcle

2008-02-19 14:20:13

Is there a way to add arrays so the user doesn't have to write them in twice?

Example

DropCrapBut := ['bones', 'iamond'];
AmmoEquip := ['ron arrow', 'teel arrow'];

How can u make it so when you declare it in a function to read it together as:

NewArray := ['bones', 'iamond','ron arrow', 'teel arrow'];

?

I was hoping for a short version to this instead of a whole function.

Negaal

2008-02-19 15:58:44

From: Narcle

I was hoping for a short version to this instead of a whole function.


You was?
Ok then it's okay! xD :p
Actually I don't know how to explain.

program New;

Function AddArrays(a, b :TStringArray) : TStringArray;
var
i, l, add : integer;
new : TStringArray;
begin
SetLength(New, Length(a) + Length(b));
L := High(A);
For i:=0 to l do
begin
New[add] := A[i];
Inc(add);
end;
L := High(B);
For i:=0 to l do
begin
New[add] := B[i];
Inc(add);
end;
Result := New
end;

var
ss, zz, nn : tstringarray;
k : integer;

begin
setlength(ss, 3)
setlength(zz, 3)
ss := ['11','22','33'];
zz := ['44','55','66'];
nn := AddArrays(ss,zz);
for k := 0 to high(nn) do
writeln(nn[k]);
end.

n3ss3s

2008-02-19 15:59:44

I don't quite understand what you mean, but you can do

NewArray := ['bones', 'iamond', 'ron arrow', 'teel arrow'];


EDIT: Oh - @Negaal: Battle?


program New;

Function CombineTSA(A, B: TStringArray): TStringArray;
Var
L, L2, I: Integer;
Begin
L := High(A);
L2 := High(B);
SetArrayLength(Result, L + 1);
Result := A;
SetArrayLength(Result, L + L2 + 2);
For I := 0 To L2 Do
Result[l + 1 + i] := b[i];
End;

Var
C: TStringArray;
I: Integer;

begin
C := CombineTSA(['a', 'b'], ['c', 'd']);
For I := 0 To High(C) Do
Writeln(C[i]);
end.

Narcle

2008-02-19 17:29:43

Figures it has to be a function… oh well lol

Thanks guys.

Sir R. M8gic1an

2008-02-20 07:57:16

yes it does have to be a function. but here's for the competition:

program RMCombineTSA;

Function RMCombineTSA(i, s:TStringArray):TStringArray;
var x, b, h:integer;
begin
Result:= i;
SetLength(Result, Length(i)+Length(s));

h := Length(Result);
b := Length(i)
for x:=b to h-1 do
Result[x] := s[x-b];
end;

var
TSA:TStringArray;
Int:Integer;
begin
TSA := RMCombineTSA(['hi', 'hello', 'hey'], ['bye', 'buh bye', 'ciao']);
for Int:=0 to High(TSA) do
writeln(TSA[Int]);
end.

question:

h := Length(Result);
for x:=b to h-1 do
Result[x] := s[x-b];

//is faster than

for x:=b to High(Result) do

//right?
~RM