http://qs321.pair.com?node_id=746626


in reply to Re^6: (Mis)Understanding "works"
in thread (Mis)Understanding grep...eq $_

Update:posted this my mistake at wrong reply level. I am agreeing with Tye and showing an example of how to use grep on "arrays".

I don't know exactly what is meant by "array" in a Perl context. There are lists and lists of lists of lists(LoL) and of course lists of lists of lists(LoLoL), Lists of Hashes (LoH), etc. grep works great on all these things! If you mean that a LoL is an "array" then here is one example of how to use grep on something like that:

#!/usr/bin/perl -w use strict; #remove "rows" that don't have a value greater than 75 my $value =75; my $rLoL = [ [78,43,87], [64,73,72], [99], [65,67,71] ]; @$rLoL = grep { (grep {$_ > $value}@$_)>=1}@$rLoL; foreach my $ref (@$rLoL) { print "@$ref\n"; } #prints #78 43 87 #99 #the inside grep above is used in a scalar context. #It basically says "do we want to keep this list or not?" #outside grep passes list ref to output based upon that #true/false decision. #grep{} works with anything that can be reduced to a #yes/no question #@LoH = grep{keys (%_) > 2}@LoH #removes hashes from List of Hashes that have fewer #than 3 keys.