Is that code an example or is that the specific instance you're
trying to replace? Because if the latter, you can just use grep:
my @array = grep $_ != 5, 0..9;
Actually this isn't exactly the same, because your code has a bug,
assuming that your goal is to weed out elements equal to 5.
(That's the goal, right? If not, never mind.)
The bug is that you're altering the array while progressing through
the loop, which means that you skip certain elements. Try your
code on this array:
my @array = qw(0 1 2 3 4 5 5 6 7 8 9 );
Only the first 5 will be removed, because after splice-ing out
the first 5, the index of the 5 following it has just decreased by
1. But the loop counter increments, and you end up skipping
the 5 following it.
As for your general question, which seems to be, is there a way
to get rid of reliance on loop counters when iterating through
arrays. And that's a good question, and I don't really think there
is a good way. There isn't a magic variable for "index into an
array" when in a foreach loop. Although perhaps there should
be; Dominus has a post about this.
All the same, though, you should probably refrain from altering
an array while iterating over it. | [reply] [Watch: Dir/Any] [d/l] [select] |
| [reply] [Watch: Dir/Any] [d/l] |
If you bracket it, the grep expression becomes a block. It can be as complicated as you please. If it evaluates true, $_ (possibly modified within the block) is placed in the returned array.
After Compline, Zaxo
Update: Corrected block <-> expr terminology.
| [reply] [Watch: Dir/Any] |
grep {$_!=5} @array;
After Compline, Zaxo | [reply] [Watch: Dir/Any] [d/l] |
If you don't care about what you throw away:
@array = grep { $_ != 5 } @array;
If you do care:
my @thrownaway;
@array = grep { if ( $_ == 5 ) { push @thrownaway, $_; 0 } else 1; } @
+array;
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] [Watch: Dir/Any] [d/l] [select] |
for $i(0..$#array) {
splice @array, $i, 1 if $array[$i] == 5;
}
Update: Got called away for dinner and didn't get a chance to finish(but it looks like others beat me anyway).
Also, it all you care about is the value of the array, i.e., not its index, you could use this:
for (@array) { ... if $_ == 5. }
| [reply] [Watch: Dir/Any] [d/l] [select] |
I wouldn't use the splice construct inside of a loop iterating over the array. It'll get rid of the right elements, but it'll keep the array the same size due to autovivification at the end. (This may depend on the version of Perl.) Running under strict exposes this:
my @array = (0 .. 9);
$" = " ";
for my $i (0 .. $#array) {
splice @array, $i, 1 if $array[$i] == 5;
print "$i: @array\n";
}
print "Array is ", scalar @array, " elements long.\n";
Bang, uninitialized value and @array is 9 elements long at the end. Yikes. | [reply] [Watch: Dir/Any] [d/l] |
And what's worse, if you have consequetive elements equal
to 5, only of them is removed. Because the
splice moves everything following one down,
but the next iteration ups the counter.
-- Abigail
| [reply] [Watch: Dir/Any] [d/l] [select] |