young_stu has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: better array plucking?
by revdiablo (Prior) on May 10, 2005 at 21:55 UTC | |
|
Re: better array plucking?
by sh1tn (Priest) on May 10, 2005 at 22:28 UTC | |
|
Re: better array plucking?
by TedPride (Priest) on May 11, 2005 at 03:59 UTC |