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![]()
one way:
If you really must do it in a loop see the splice() function. But grep() is very good for this type of thing.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";
Thanks 4 the reply...
but in my case i don't know the values, i just know the index of each value...
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";
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks