Results 1 to 9 of 9

Thread: Delete element from an array

  1. #1
    Join Date
    Oct 2006
    Posts
    468
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Delete element from an array

    I think they're called elements...

    I'm wondering how I can delete an entire section from an array. For instance, I want to delete MyArray[2], not the value in it, but the entire MyArray[2]. Is this possible and if so, does the length of the array shorten down a value as well?

  2. #2
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Can you expand more?

    These are your 'deleting' functions in SRL:

    http://www.villavu.com/repositories/...core/Math.scar

  3. #3
    Join Date
    Apr 2007
    Location
    Melbourne, Aus
    Posts
    202
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    he wants to know how to delete an array variable he has coded into the script. To free up memory i presume, I dont think it can be done.

  4. #4
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    procedure DeleteIndex(var Arr : TVariantArray; Index : integer);
    var
      h : integer;
    begin
      h := high(Arr);
      Swap(Arr[h], Arr[Index]);
      SetArrayLength(Arr, h);
    end;
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  5. #5
    Join Date
    Apr 2007
    Location
    Melbourne, Aus
    Posts
    202
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    E: LOL. my bad i just realised what u did there.. wow.. im gonna blame that on tiredness :P

  6. #6
    Join Date
    Oct 2006
    Posts
    468
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Ok, I'll try to explain it a bit better.

    Lets say I have

    SCAR Code:
    var
      MyArray: Array of Integer;

    begin
      SetArrayLength(MyArray, 3);
      MyArray[0] := 1;
      MyArray[1] := 2;
      MyArray[2] := 3;
    end;

    I want to remove, for example, the whole entire MyArray[1] so I will end up with

    SCAR Code:
    MyArray[0] := 1;
      MyArray[1] := 3;

    Notice how the length of the array changed and the original MyArray[2] moved to fill in MyArray[1]? That's what I want to do. Will DeleteValueInIntArray do this?

  7. #7
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by bbri06 View Post
    Ok, I'll try to explain it a bit better.

    Lets say I have

    Code:
    var
    MyArray: Array of Integer;

    begin
    SetArrayLength(MyArray, 3);
    MyArray[0] := 1;
    MyArray[1] := 2;
    MyArray[2] := 3;
    end;


    I want to remove, for example, the whole entire MyArray[1] so I will end up with

    Code:
    MyArray[0] := 1;
    MyArray[1] := 3;


    Notice how the length of the array changed and the original MyArray[2] moved to fill in MyArray[1]? That's what I want to do. Will DeleteValueInIntArray do this?
    Yes it will.
    Don't use my code it has a flaw, sorry (it's 3:38am ).

    Oh and by the way, you could have just done this:
    SCAR Code:
    var
      MyArray : TIntegerArray;
    begin
      MyArray := [1, 2, 3];
    end.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  8. #8
    Join Date
    Oct 2006
    Posts
    468
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Ok, thanks.

    Off topic: I love how I get quick responses on this forum at 3:40 AM

  9. #9
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    If you'd want to keep the order of the array as it is before deleting an element, then you'd have to loop through all the elements and set the element that is before the current index to index, meaning something like this:

    SCAR Code:
    procedure DeleteElement(var Arr : TVariantArray; Index : Integer);
    var
      H, I : Integer;
    begin
      H := High(Arr);
      for I := Index-1 to H-1 do
        Arr[I] := I+1;
      SetArrayLength(Arr, High(Arr));
    end;
    I wrote this in this textbox and I haven't tested it or anything, but I hope you get the idea...

    EDIT: There is already a procedure like this in Math.scar (as Naum already said)...
    procedure DeleteValueInIntArray(var Arr: TIntegerArray; ValuePosition: Integer);

Thread Information

Users Browsing this Thread

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

Posting Permissions

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