and this array contains some elements that I want to get rid of. Further, I don't know which indices of the array contain the elements I want to expunge, and I want the final array I end up with to contain no empty indices--i.e. I want to ``shorten" the array by 1 everytime I expunge the contents of any of its indices. The way to do this that strikes me as obvious seems inelegant:my @a
where is_unwanted is a sub that tests to find out if the contents of an index are of a sort I want to get rid of. Is there a way to do this without creating this extra @new_a array? A (naive) use of splice won't work, since splicing an array changes the identity of the indices with each call. E.g.my @new_a; for my $i (0..$#a){ push (@new_a, $a[$i]) unless is_unwanted($a[$i]); } @a = @new_a; @new_a = ();
Thanks!my @a = ("bob", "bob", "martha", "bob"); for my $i (0..$#a){ splice (@a, $i, 1) if $a[$i] =~ m/bob/; } #@a is now ("bob", "martha"), but I don't want any "bob"s; #also I get harassed about uniniatialized values when I use warnings
In reply to better array plucking? by young_stu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |