Closed Thread
Results 1 to 4 of 4

Thread: removing index of array of no values

  1. #1
    mhadidi2002 is offline Newbie
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Exclamation removing index of array of no values

    Dear All,

    I have a question...
    if i have an array
    @arr = (1,3,2.5,4,3.5,7,6,9,12,13,15,17);
    size = 12

    then i deleted the first 2 elements (1,3) & the 4th & 5th element (4,3.5)
    so $arr[0],$arr[1],$arr[3] & $arr[4] are now empty.

    I would like to make the elements shifted to the left, so its size reduced to be 8 & the first element arr[0] will be 2.5, & the last element to be 17 arr[7].

    I am applying some conditions that removes elements from array in a for loop, after this, the array has gaps (index with no elements), but i would like to remove this gaps.

    Thank you in advance

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: removing index of array of no values

    one way:

    Code:
    @arr = (1,3,2.5,4,3.5,7,6,9,12,13,15,17);
    @arr = grep(!/^(1|3|4|3\.5)$/,@arr);
    print "@arr";
    If you really must do it in a loop see the splice() function. But grep() is very good for this type of thing.

  4. #3
    mhadidi2002 is offline Newbie
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Re: removing index of array of no values

    Thanks 4 the reply...

    but in my case i don't know the values, i just know the index of each value...

  5. #4
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: removing index of array of no values

    Then all you need to do is use an array slice:

    Code:
    @arr = (1,3,2.5,4,3.5,7,6,9,12,13,15,17);
    @arr = @arr[2,5..$#arr];
    print "@arr";

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. using a variable as array index
    By jackson6612 in forum C and C++
    Replies: 4
    Last Post: 10-09-2011, 01:18 PM
  2. Replies: 1
    Last Post: 02-12-2011, 10:17 PM
  3. Replies: 7
    Last Post: 01-24-2010, 10:02 PM
  4. Using array index
    By gaylo565 in forum C# Programming
    Replies: 3
    Last Post: 05-16-2008, 09:19 AM

Tags for this Thread

Bookmarks

Posting Permissions

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