What is the best way to delete a value from an array in Perl?
The array has lots of data and I need to delete two elements.
Below is the code snippet I am using,
my @array = (1,2,3,4,5,5,6,5,4,9);
my $element_omitted = 5;
@array = grep { $_ != $element_omitted } @array;
Solution 1:
Use splice if you already know the index of the element you want to delete.
Grep works if you are searching.
If you need to do a lot of these, you will get much better performance if you keep your array in sorted order, since you can then do binary search to find the necessary index.
If it makes sense in your context, you may want to consider using a "magic value" for deleted records, rather then deleting them, to save on data movement -- set deleted elements to undef, for example. Naturally, this has its own issues (if you need to know the number of "live" elements, you need to keep track of it separately, etc), but may be worth the trouble depending on your application.
Edit Actually now that I take a second look -- don't use the grep code above. It would be more efficient to find the index of the element you want to delete, then use splice to delete it (the code you have accumulates all the non-matching results..)
my $index = 0;
$index++ until $arr[$index] eq 'foo';
splice(@arr, $index, 1);
That will delete the first occurrence. Deleting all occurrences is very similar, except you will want to get all indexes in one pass:
my @del_indexes = grep { $arr[$_] eq 'foo' } 0..$#arr;
The rest is left as an excercise for the reader -- remember that the array changes as you splice it!
Edit2 John Siracusa correctly pointed out I had a bug in my example.. fixed, sorry about that.
Solution 2:
splice will remove array element(s) by index. Use grep, as in your example, to search and remove.
Solution 3:
Is this something you are going to be doing a lot? If so, you may want to consider a different data structure. Grep is going to search the entire array every time and for a large array could be quite costly. If speed is an issue then you may want to consider using a Hash instead.
In your example, the key would be the number and the value would be the count of elements of that number.
Solution 4:
if you change
my @del_indexes = grep { $arr[$_] eq 'foo' } 0..$#arr;
to
my @del_indexes = reverse(grep { $arr[$_] eq 'foo' } 0..$#arr);
This avoids the array renumbering issue by removing elements from the back of the array first. Putting a splice() in a foreach loop cleans up @arr. Relatively simple and readable...
foreach $item (@del_indexes) {
splice (@arr,$item,1);
}