> -----Original Message-----
> From: Steven Surgnier [mailto:ssurgn...@gmail.com]
> Sent: Tuesday, July 19, 2011 3:24 PM
> To: beginners@perl.org
> Subject: grep question
> 
> Hi,
> 
> I desire a concise conditional statement which simply checks if each
> entry
> in an array matches a given string.  For example:
> 
> print "all the same" if (grep {m/test_name/} @bins) == scalar @bins);
> 
> #END CODE
> 
> I thought, "grep will return the number of matches, so why not just
> check to
> see if it equals the length of the array?"
> I'm open to any suggestions even if it's merely cosmetic.
> 
> Sincerely,
> 
> Steven

Your example is close, but you have too many right parentheses.
Others have indicated different (probably more efficient) ways to do it.
Also, you do not need the match (m) operator when using the standard regular
expressions delimiters.
I also think 'all the same' is misleading. If you want to check that all the
strings are actually the same, the regular expression would be different.

use strict;
use warnings;

my @array1 = qw(test_name2 test_name4 test_name6 test_name8);
my @array2 = qw(1 3 test_name5 test_name7 9);

print "all contain test_name: array1" if (grep {/test_name/} @array1) ==
scalar @array1;
print "all contain test_name: array2" if (grep {/test_name/} @array2) ==
scalar @array2;

HTH,
Ken




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to