grep question
am 19.07.2011 21:23:37 von Steven Surgnier
--0016e6d7df0178018104a8710d85
Content-Type: text/plain; charset=ISO-8859-1
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
--0016e6d7df0178018104a8710d85--
Re: grep question
am 19.07.2011 21:45:06 von Tessio Fechine
--00163676588444ef8f04a8715a89
Content-Type: text/plain; charset=ISO-8859-1
Hello,
you can use
print "all the same" unless (grep {! /test_name/} @bin);
2011/7/19 Steven Surgnier
> 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
>
--00163676588444ef8f04a8715a89--
Re: grep question
am 19.07.2011 21:55:37 von Brian Fraser
--bcaec51a72b4ebba6704a8717f71
Content-Type: text/plain; charset=UTF-8
On Tue, Jul 19, 2011 at 4:23 PM, Steven Surgnier wrote:
> 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
>
use List::MoreUtils qw( all );
print "all the same" if all { /test_name/ } @bins;
Which has the added bonus of stopping early if one of them doesn't match,
while grep has to go through the entire array in either case.
--bcaec51a72b4ebba6704a8717f71--
RE: grep question
am 19.07.2011 22:01:27 von Ken Slater
> -----Original Message-----
> From: Steven Surgnier [mailto:ssurgnier@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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: grep question
am 20.07.2011 00:49:59 von jwkrahn
Steven Surgnier wrote:
> 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.
I would just write that as:
print "all the same" if @bins == grep /test_name/, @bins;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: grep question
am 20.07.2011 01:21:13 von rvtol+usenet
On 2011-07-19 21:23, Steven Surgnier wrote:
> 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);
Your match is not anchored, so ITYM:
my $s = $bins[0];
print qq{all '$s'} unless grep( $_ ne $s, @bins[1..$#bins]);
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/