SRL Archive - SRL Archives

RM - Array Work


Sir R. M8gic1an

2008-02-20 08:55:04

hey, made a couple array functions that could be useful:
BubbleSort (from Pascal) // scar already has this
Switch (from Pascal) // scar has this as swap(Arrayindex[1], ArrayIndex[2])
DeleteRecord
RMCombineTSA

BubbleSort sorts your array, from the smallest to the biggest int

Switch will change the position of the values in an array

DeleteRecord will delete a value from an array, and shorten it.

RMCombineTSA will make two TStringArrays into one.


you can run he whole thing to test it.

procedure Switch(s:TIntegerArray; a, b:integer); //by RM, but common in pascal
var temp:integer;
begin
temp := s[a];
s[a] := s[b];
s[b] := temp;
end;

//DelRecord(i, 3);
//i[3] disappears and is replaced by i[4];
Function DelRecord(s:TIntegerArray; a:integer):TIntegerArray; //by RM
var x, y:integer;
begin
y:=High(s)-1;
for x:= a to y do
Switch(s, x, x+1);
SetArrayLength(s, Length(s)-1);
Result:= s;
end;

Procedure BubbleSort(numbers : TIntegerArray); // common in pascal, not by me
Var i, j, temp, size : Integer;
Begin
size:= Length(numbers)-1
For i := size DownTo 1 do
For j := 1 to i do
If (numbers[j-1] > numbers[j]) then
Begin
temp := numbers[j-1];
numbers[j-1] := numbers[j];
numbers[j] := temp;
End;

end;

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
i, b:TIntegerArray;
TSA:TStringArray;
x:integer;
begin
ClearDebug;
i:=[0, 10, 4, 8, 2, 6]
BubbleSort(i);
writeln('Sorting');
writeln('');
for x:=0 to High(i) do
writeln('x: '+IntToStr(x)+' - '+IntToStr(i[x]));

switch(i, 0, 2);
writeln('');
writeln('switch 0 with 2');
writeln('');
for x:=0 to High(i) do
writeln('y: '+IntToStr(x)+' - '+IntToStr(i[x]));

i := DelRecord(i, 2);
writeln('');
writeln('Deleting record 2');
writeln('');
for x:=0 to High(i) do
writeln('z: '+IntToStr(x)+' - '+IntToStr(i[x]));

wait(50);
writeln('combining "hello" and "hi" with "ciao" and "bye" using CombineTSA');
TSA := RMCombineTSA(['hello', 'hi'], ['ciao', 'bye']);
wait(2);
for x:=0 to High(TSA) do
writeln('TSA['+IntToStr(x)+']: '+TSA[x]);
end.


~RM

n3ss3s

2008-02-20 09:11:47

1. SCAR already has BubbleSort
2. Switch can be done by Swap(ArrayIndex[x], ArrayIndex[y]);
3. Sorry, nvm, your Result[<stuffhere>] is different…

Mine:


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;


Good job anyways

Sir R. M8gic1an

2008-02-20 09:24:25

oh i didn't know about Bubblesort. and i tried switch, which i though would work here, didn't know about swap.

n yes i saw your procedure earlier but since i had done DelRecord i though i might add mine in ;)

~RM

n3ss3s

2008-02-20 10:29:42

Yea, but my first point was that yours was like.. > 90% copy of mine except that length setting order and var names were different, but saw that your Result[<index>] was different…