SRL Archive - OSR Help

Removing integer from TIntegerArray


Cstrike

2010-08-13 13:34:54

Lets say we have


// Exp : TIntegerArray;
Exp := [50, 55, 60, 70]

I want to remove Exp[2], so that the array would read:


Exp := [50, 55, 70]

How would I go about doing this? If it's even possible…
The only way I was thinking to avoid it reading a part was to replace it with -1 and then in the script put "if -1 then ignore", but that would be tons of extra code :( Thanks

Wanted

2010-08-13 15:47:18

Might want to check Wizzy plugin or w/e to check to see if there already is this, but here's the basic concept

procedure DeleteIntInArr(var Arr: TIntegerArray; Index: Integer);
var
TempArr: TIntegerArray;
I, L: Integer;
begin
SetLength(TempArr, Length(Arr) - 1);
for I := 0 to High(Arr) do
if (I <> Index) then
begin
TempArr[L] := Arr[I];
Inc(L);
end;
Arr := TempArr;
end;

Cstrike

2010-08-13 16:18:07

Thanks very much IceFire :)

I looked through WizzyPlugin for TInteger removal and wasn't able to find any (should I have been looking for TPoint maybe? Are they interchangable?)


One last question with the above code would be:
program new;
var
NumbersArray : TIntegerArray;
i : Integer;

procedure DeleteIntInArr(var Arr: TIntegerArray; Index: Integer);
var
TempArr: TIntegerArray;
I, L: Integer;
begin
SetLength(TempArr, Length(Arr) - 1);
for I := High(Arr) downto 0 do
if (I <> Index) then
begin
TempArr[L] := Arr[I];
Inc(L);
end;
Arr := TempArr;
end;

begin
NumbersArray := [10, 20, 30, 40, 50, 60];
//0 1 2 3 4 5
DeleteIntInArr (NumbersArray, 3); // Get rid of 40

for i:=0 to High(NumbersArray) do
begin
writeln (IntToStr(i)+': '+ IntToStr(NumbersArray[i]));
end;
end.

The result is:
Compiled succesfully in 16 ms.
0: 60
1: 50
2: 30
3: 20
4: 10
Successfully executed.



So I took your code and modified the 'for to do' line to this:
procedure DeleteIntInArr(var Arr: TIntegerArray; Index: Integer);
var
TempArr: TIntegerArray;
I, L: Integer;
begin
SetLength(TempArr, Length(Arr) - 1);
for I := 0 to High(Arr) do // <-- Modded here
if (I <> Index) then
begin
TempArr[L] := Arr[I];
Inc(L);
end;
Arr := TempArr;
end;
Would this modification cause me any problem down the road?
I'm still trying to understand the code, I generally get it but I'm going over it in fine detail as best as I can, so hopefully you can forgive any error in knowledge on my part! :)

Boreas

2010-08-13 16:20:59

In cases where the order does not have to be maintained and the array is quite long, you can set Array[target] to the value of Array[High], and then set the array length to be 1 shorter.

Wanted

2010-08-13 16:21:50

Yea whoops

ZephyrsFury

2010-08-14 04:27:23

With SRL included you can use these:


// * procedure DeleteValueInStrArray(var Arr: TStringArray; ValuePosition: Integer); // * by EvilChicken!
// * procedure DeleteValueInIntArray(var Arr: TIntegerArray; ValuePosition: Integer); // * by EvilChicken!
// * procedure DeleteValueInFloatArray(var Arr: TExtendedArray; ValuePosition: Integer); // * by EvilChicken!
// * procedure DeleteValueInBoolArray(var Arr: TBooleanArray; ValuePosition: Integer); // * by EvilChicken!

The functions are in Math.scar and delete a specific array element whilst also maintaining the array order.