Wednesday, January 23, 2008

how to keep track of index in perl grep

well the idea is that
map { express ? $_:() } @list;
is equivalent to
grep exp @list;

so therefore

#!/usr/bin/perl -w
use strict;

my @blug = qw (apple bug orange twist app);

# now create anonymous array reference including the array index

my $i=0;
my @newarray;
foreach (@blug) {
push ( @newarray, [$_,$i] );
$i++;
}

print "now this certainly works \n";
foreach (@newarray) {
print $$_[0] ."\n";
}



my @out= map {$$_[0]=~/app/ ? $_:() } @newarray;


print "and we are done \n";
foreach (@out) {
print $$_[0] ."\n";
}

}

No comments: