[Chicago-talk] map and pop
Steven Lembark
lembark at wrkhors.com
Tue Dec 30 16:48:56 CST 2003
-- "Dooley, Michael" <Dooley.Michael at con-way.com>
> I want to do something like this, but I also want to remove the found
> element from @files.
>
> my @nfiles=grep /^car/, @files;
>
> can anyone point me in the right direction, I am thinking map is needed
> but I don't have much confidence using it.
my @nfiles = ();
@files = map { /$regex/ ? do {push @nfiles, $_; ()} : $_ } @files;
e.g.,
DB<7> @a = (1..10)
DB<8> @b = ()
DB<10> x @a = map { $_ % 2 ? do{push @b, $_; ()} : $_ } @a
0 2
1 4
2 6
3 8
4 10
DB<11> x @b
0 1
1 3
2 5
3 7
4 9
Other way would be a hash, which might be a bit easier for
anyone who follows you to figure out:
sub grepalize
{
my $list = shift;
my $regex = shift;
my @found = grep /$regex/, @$list;
my %scratch = map { $_ => 1 } @list;
delete @scratch{@found};
@$list = keys %scratch;
\@found
}
my $nfiles = grepalize \@files, qr/^car/;
# at this point anything in $fnfiles has been
# removed from @files.
--
Steven Lembark 2930 W. Palmer
Workhorse Computing Chicago, IL 60647
+1 888 359 3508
More information about the Chicago-talk
mailing list