in reply to walking an array and unshifting
@list = grep { $_ != 2 && $_ != 4 } @list;Or, you can go all Old School and do it this way:
The redo is important because it prevents the for loop from incrementing $i and thereby skipping an entry. This way, you're keeping pretty "close to the metal" and nothing will slip by.my @foo = 1..10; for (my $i = 0; $i < @foo; $i++) { print "$i ($foo[$i])\n"; if ($foo[$i] == 5 || $foo[$i] == 6) { splice(@foo, $i, 1); redo; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Re: walking an array and unshifting
by merlyn (Sage) on Jun 09, 2002 at 15:52 UTC |