Results 1 to 8 of 8

Thread: Erasing equal integers on different integer Arrays

  1. #1
    Join Date
    Mar 2013
    Location
    Argentina
    Posts
    758
    Mentioned
    27 Post(s)
    Quoted
    365 Post(s)

    Default Erasing equal integers on different integer Arrays

    How would i go about doing this? (im using Aerolib)

    I have pizzas and 1/2 pizzas and i want to drop everything but them, and slots 1 and 2 from the inventory.

    I have searched for a function to delete repeated integers between 2 integer arrays and havent found any.

    Please enlighten me scripting gods

    Simba Code:
    procedure dropStuff();
    var
      i: integer;
      pizza, pizza2, usedSlots, dropSlots : TintegerArray;
    begin

      pizza := item_pizza.getSlots;
      pizza2 := item_pizza2.getSlots;

      dropSlots := [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ,26 ,27 ,28];
      usedSlots := combineintArray(pizza, pizza2);
     

      //Delete usedSlots from dropSlots

      for i = 3 to 28 do
      begin
        if inIntArray(dropSlots, i) then
          fastDropSlots([I]);
      end;

    end;
    Formerly known as Undorak7

  2. #2
    Join Date
    Jun 2013
    Location
    Scranton
    Posts
    496
    Mentioned
    5 Post(s)
    Quoted
    220 Post(s)

    Default

    i think combining the two arrays, then using ClearSameIntegers() would be a lazy way to do it

  3. #3
    Join Date
    Mar 2013
    Location
    Argentina
    Posts
    758
    Mentioned
    27 Post(s)
    Quoted
    365 Post(s)

    Default

    Quote Originally Posted by jstemper View Post
    i think combining the two arrays, then using ClearSameIntegers() would be a lazy way to do it
    this converts an integer array thats [13, 13, 14, 14] into [13, 14]. It does not remove the number from the array. But nice idea

    i tried this to clear the values, but im only managing to get some of the values cleared, it will drop pizzas sometimes.

    Simba Code:
    for i := 0 to 28 do
      begin
        if inIntArrayEx(dropSlots, i, (usedSlots[i])) then
          DeleteValueInIntArray(dropSlots, i);
      end;
    Formerly known as Undorak7

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Live Test: https://ideone.com/

    Pascal Code:
    program meh;

    type TIntegerArray = array of Integer;

    procedure Delete(var arr: TIntegerArray; index: Integer);
    var
      i: Integer;
    begin
      if (index >= 0) and (index < Length(arr)) then
      begin
        for i := index + 1 to high(arr) do
        begin
          arr[i - 1] := arr[i];
        end;
     
        SetLength(arr, Length(arr) - 1);
      end;
    end;

    function IndexOf(var arr: TIntegerArray; value: Integer): Integer;
    var
      i: Integer;
    begin
      for i := 0 to high(arr) do
        if arr[i] = value then
          exit(i);
         
      exit(-1);
    end;

    procedure Delete(var arr: TIntegerArray; toDelete: TIntegerArray);
    var
      i, j: Integer;
    begin
      for i := 0 to high(toDelete) do
      begin
        j := indexOf(arr, toDelete[i]);
        if j >= 0 then
          Delete(arr, j);
      end;
    end;

    var
      A: TIntegerArray;
      B: TIntegerArray;
      i: Integer;
    begin

      A := TIntegerArray.Create(1, 2, 3, 4, 5);
      B := TIntegerArray.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
     
      Delete(B, A);
     
      for i := 0 to high(B) do
        writeln(B[i]);
    end.


    Explanation..

    To delete from an array, you need to perform a shift or a swap.. For example:

    Simba Code:
    arr := [1, 2, 3, 4, 5, 6, 7];

    //We want to delete index 2. -- Value of 3.
    //Therefore we shift from that index + 1 down the line until there is no space in between and the last element is duplicated..

    arr := [1, 2, 3, 4, 5, 6, 7];
    arr := [1, 2, 4, 4, 5, 6, 7];
    arr := [1, 2, 4, 5, 5, 6, 7];
    arr := [1, 2, 4, 5, 6, 6, 7];
    arr := [1, 2, 4, 5, 6, 7, 7];


    //Note that I added steps above to sorta show the elements moving..
    //In reality a single for-loop will actually do:
    arr := [1, 2, 3, 4, 5, 6, 7];
    arr := [1, 2, 4, 5, 6, 7, 7];

    //Now that the 3 is gone, we need to delete the last element.

    arr := [1, 2, 4, 5, 6, 7];

    Alternatively, you can do a swap:
    Simba Code:
    arr := [1, 2, 3, 4, 5, 6, 7];

    //We want to delete index 2. -- Value of 3.
    //Therefore we swap from that index up the line until the last element is the one we want deleted..

    arr := [1, 2, 3, 4, 5, 6, 7];
    arr := [1, 2, 4, 3, 5, 6, 7];
    arr := [1, 2, 4, 5, 3, 6, 7];
    arr := [1, 2, 4, 5, 6, 3, 7];
    arr := [1, 2, 4, 5, 6, 7, 3];

    //Again, a single for-loop will do fine..
    //Now that the 3 is gone, we need to delete the last element.

    arr := [1, 2, 4, 5, 6, 7];

    You do this for every element you want to remove.. The swap is actually less efficient because swapping requires a temporary variable or xor swap (more instructions).. but in any case, you won't notice a difference.. Choose whichever method you understand.
    Last edited by Brandon; 10-08-2017 at 11:56 PM.
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Jan 2012
    Location
    Sydney, Australia
    Posts
    877
    Mentioned
    12 Post(s)
    Quoted
    368 Post(s)

    Default

    Give this a try.

    Simba Code:
    procedure dropStuff();
    var
      i: integer;
      pizzas, dropSlots : TintegerArray;

    begin
      // We find the inventory slots for full & half pizzas and store them in a TIntegerarray
      // We then remove the duplicates and sort them from lowest to highest.

      pizzas := combineintArray(item_pizza.getSlots, item_pizza2.getSlots);
      ClearSameIntegers(pizzas);
      Quicksort(pizzas);
      writeln('Pizza slots:', pizzas);

      // We go through slots 3 to 28 and check them against the pizzas array.
      // If an item exists in the slot, and the slot we are looking at doesn't match any integers in pizzas, we store it in dropSlots.

      for i := 3 to 28 do
        if ItemInSlot(i) then
          if not InIntArray(pizzas, i) then
          begin
            SetArrayLength(dropSlots, Length(dropSlots) + 1);
            dropSlots[high(dropSlots)] := i;
          end;
      writeln('Slots to drop are: ', dropSlots);

      //We then drop the items that we added to the dropSlots array.

      fastDropSlots(dropSlots); //Drops the items.
    end;

    Not tested in game. Tested the Writeln output with some predetermined integer arrays and it seemed to work ok.

    It basically builds a TIntegerArray that is comprised of the slots that we want to drop.
    Last edited by Dan the man; 10-09-2017 at 01:56 AM.

  6. #6
    Join Date
    Mar 2013
    Location
    Argentina
    Posts
    758
    Mentioned
    27 Post(s)
    Quoted
    365 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    Live Test: https://ideone.com/

    Pascal Code:
    program meh;

    type TIntegerArray = array of Integer;

    procedure Delete(var arr: TIntegerArray; index: Integer);
    var
      i: Integer;
    begin
      if (index >= 0) and (index < Length(arr)) then
      begin
        for i := index + 1 to high(arr) do
        begin
          arr[i - 1] := arr[i];
        end;
     
        SetLength(arr, Length(arr) - 1);
      end;
    end;

    function IndexOf(var arr: TIntegerArray; value: Integer): Integer;
    var
      i: Integer;
    begin
      for i := 0 to high(arr) do
        if arr[i] = value then
          exit(i);
         
      exit(-1);
    end;

    procedure Delete(var arr: TIntegerArray; toDelete: TIntegerArray);
    var
      i, j: Integer;
    begin
      for i := 0 to high(toDelete) do
      begin
        j := indexOf(arr, toDelete[i]);
        if j >= 0 then
          Delete(arr, j);
      end;
    end;

    var
      A: TIntegerArray;
      B: TIntegerArray;
      i: Integer;
    begin

      A := TIntegerArray.Create(1, 2, 3, 4, 5);
      B := TIntegerArray.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
     
      Delete(B, A);
     
      for i := 0 to high(B) do
        writeln(B[i]);
    end.


    Explanation..

    To delete from an array, you need to perform a shift or a swap.. For example:

    Simba Code:
    arr := [1, 2, 3, 4, 5, 6, 7];

    //We want to delete index 2. -- Value of 3.
    //Therefore we shift from that index + 1 down the line until there is no space in between and the last element is duplicated..

    arr := [1, 2, 3, 4, 5, 6, 7];
    arr := [1, 2, 4, 4, 5, 6, 7];
    arr := [1, 2, 4, 5, 5, 6, 7];
    arr := [1, 2, 4, 5, 6, 6, 7];
    arr := [1, 2, 4, 5, 6, 7, 7];


    //Note that I added steps above to sorta show the elements moving..
    //In reality a single for-loop will actually do:
    arr := [1, 2, 3, 4, 5, 6, 7];
    arr := [1, 2, 4, 5, 6, 7, 7];

    //Now that the 3 is gone, we need to delete the last element.

    arr := [1, 2, 4, 5, 6, 7];

    Alternatively, you can do a swap:
    Simba Code:
    arr := [1, 2, 3, 4, 5, 6, 7];

    //We want to delete index 2. -- Value of 3.
    //Therefore we swap from that index up the line until the last element is the one we want deleted..

    arr := [1, 2, 3, 4, 5, 6, 7];
    arr := [1, 2, 4, 3, 5, 6, 7];
    arr := [1, 2, 4, 5, 3, 6, 7];
    arr := [1, 2, 4, 5, 6, 3, 7];
    arr := [1, 2, 4, 5, 6, 7, 3];

    //Again, a single for-loop will do fine..
    //Now that the 3 is gone, we need to delete the last element.

    arr := [1, 2, 4, 5, 6, 7];

    You do this for every element you want to remove.. The swap is actually less efficient because swapping requires a temporary variable or xor swap (more instructions).. but in any case, you won't notice a difference.. Choose whichever method you understand.
    Quote Originally Posted by Dan the man View Post
    Give this a try.

    Simba Code:
    procedure dropStuff();
    var
      i: integer;
      pizzas, dropSlots : TintegerArray;

    begin
      // We find the inventory slots for full & half pizzas and store them in a TIntegerarray
      // We then remove the duplicates and sort them from lowest to highest.

      pizzas := combineintArray(item_pizza.getSlots, item_pizza2.getSlots);
      ClearSameIntegers(pizzas);
      Quicksort(pizzas);
      writeln('Pizza slots:', pizzas);

      // We go through slots 3 to 28 and check them against the pizzas array.
      // If an item exists in the slot, and the slot we are looking at doesn't match any integers in pizzas, we store it in dropSlots.

      for i := 3 to 28 do
        if ItemInSlot(i) then
          if not InIntArray(pizzas, i) then
          begin
            SetArrayLength(dropSlots, Length(dropSlots) + 1);
            dropSlots[high(dropSlots)] := i;
          end;
      writeln('Slots to drop are: ', dropSlots);

      //We then drop the items that we added to the dropSlots array.

      fastDropSlots(dropSlots); //Drops the items.
    end;

    Not tested in game. Tested the Writeln output with some predetermined integer arrays and it seemed to work ok.

    It basically builds a TIntegerArray that is comprised of the slots that we want to drop.
    Thank you both! have it working now
    Formerly known as Undorak7

  7. #7
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    In this case, as there is no 'quick' native function such as "RemoveTIAfromTIA I would just inverse the input:

    Simba Code:
    procedure dropStuff();
    var
      i: integer;
      pizza, pizza2, usedSlots, dontDropSlots : TintegerArray;
    begin

      pizza := item_pizza.getSlots;
      pizza2 := item_pizza2.getSlots;

      dontDropSlots := [1,2];
      usedSlots := combineintArray(pizza, pizza2);
      usedSlots := combineintArray(usedSlots, dontDropSlots);
     

      for i = 1 to 28 do
      begin
        if not inIntArray(usedSlots, i) then
          fastDropSlots([I]);
      end;

    end;

    Unless you really want those items to drop in array... then you can do:
    Simba Code:
    procedure dropStuff();
    var
      i,c: integer;
      pizza, pizza2, usedSlots, dontDropSlots, dropSlots : TintegerArray;
    begin

      pizza := item_pizza.getSlots;
      pizza2 := item_pizza2.getSlots;

      dontDropSlots := [1,2];
      usedSlots := combineintArray(pizza, pizza2);
      usedSlots := combineintArray(usedSlots, dontDropSlots);
     
      for i = 1 to 28 do
      begin
        if not inIntArray(usedSlots, i) then
        begin
          inc(c);                   // c is length counter
          SetLength(dropSlots,c);   // increase array length by 1
          dropSlots[i-1] := i;      // add element to the end
        end;
      end;

      fastDropSlots(dropSlots);
    end;

    @edit

    Now I see Dan made basically the same thing.
    Last edited by bg5; 10-11-2017 at 11:38 PM.

  8. #8
    Join Date
    Jan 2012
    Location
    Sydney, Australia
    Posts
    877
    Mentioned
    12 Post(s)
    Quoted
    368 Post(s)

    Default

    Quote Originally Posted by bg5 View Post
    In this case, as there is no 'quick' native function such as "RemoveTIAfromTIA I would just inverse the input:

    Simba Code:
    procedure dropStuff();
    var
      i: integer;
      pizza, pizza2, usedSlots, dontDropSlots : TintegerArray;
    begin

      pizza := item_pizza.getSlots;
      pizza2 := item_pizza2.getSlots;

      dontDropSlots := [1,2];
      usedSlots := combineintArray(pizza, pizza2);
      usedSlots := combineintArray(usedSlots, dontDropSlots);
     

      for i = 1 to 28 do
      begin
        if not inIntArray(usedSlots, i) then
          fastDropSlots([I]);
      end;

    end;

    Unless you really want those items to drop in array... then you can do:
    Simba Code:
    procedure dropStuff();
    var
      i,c: integer;
      pizza, pizza2, usedSlots, dontDropSlots, dropSlots : TintegerArray;
    begin

      pizza := item_pizza.getSlots;
      pizza2 := item_pizza2.getSlots;

      dontDropSlots := [1,2];
      usedSlots := combineintArray(pizza, pizza2);
      usedSlots := combineintArray(usedSlots, dontDropSlots);
     
      for i = 1 to 28 do
      begin
        if not inIntArray(usedSlots, i) then
        begin
          inc(c);                   // c is length counter
          SetLength(dropSlots,c);   // increase array length by 1
          dropSlots[i-1] := i;      // add element to the end
        end;
      end;

      fastDropSlots(dropSlots);
    end;

    @edit

    Now I see Dan made basically the same thing.

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
  •