in reply to Trouble with loops
To weed an array, use grep. To make your desired deletion from @array,
@array = grep {$_ ne 'joby'} @array;To get your desired output while deleting, I recommend printf in the grep block:
use strict; use warnings; my @array = ("joby", "andy", "ben", "tom", "bob"); sub ok {$_ ne 'joby'} print "start\n"; print map {"$_\n"} @array; print "\nmiddle\n"; my $fmt = "%s %s\n"; @array = grep { my $ok = ok(); printf $fmt, $_, $ok? 'ok': 'deleted'; $ok; } @array; print "\nend\n"; print map {"$_\n"} @array;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Trouble with loops
by jobybett (Initiate) on Jul 01, 2008 at 16:26 UTC |