Results 1 to 6 of 6

Thread: Removing integer from TIntegerArray

  1. #1
    Join Date
    May 2007
    Location
    Everywhere
    Posts
    1,428
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default Removing integer from TIntegerArray

    Lets say we have

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

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

    SCAR Code:
    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

  2. #2
    Join Date
    Dec 2006
    Location
    Houston, TX USA
    Posts
    4,791
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

    SCAR Code:
    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;
    Last edited by Wanted; 08-13-2010 at 08:22 PM.

  3. #3
    Join Date
    May 2007
    Location
    Everywhere
    Posts
    1,428
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    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:
    SCAR Code:
    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:
    SCAR Code:
    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!

  4. #4
    Join Date
    Sep 2006
    Posts
    5,219
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    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.

  5. #5
    Join Date
    Dec 2006
    Location
    Houston, TX USA
    Posts
    4,791
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Yea whoops

  6. #6
    Join Date
    Apr 2007
    Location
    Perth, Australia
    Posts
    3,926
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

    With SRL included you can use these:

    SCAR Code:
    // * 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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •