in reply to Re: Weird grep behaviour
in thread Weird grep behaviour
grep will return the whole list or an empty list depending on whether $var evaluates to true or false. Consider:
my @arr1 = ('a'); foreach my $var1 (0, 1) { if( grep($var1, @arr1) ) { print "match $var1\n"; } else { print "fail $var1\n"; } }
Update: Here's a sample usage:
#!/usr/bin/perl use warnings;use strict; my $paid = 0; # :-( my @shopping = ( 'twinkies','coffee', (grep !$paid => 'gruel', 'cabbage', 'cordial'), (grep $paid => 'caviar', 'truffles', 'champagne'), ); print "@shopping";
|
|---|