in reply to Some sexy regex
grep applies the expression(s) in the first argument (either a single expression followed by a comma, or a block { expr1; expr2; .. exprN; } which is not followed by a comma) to each element of the list specified in the second argument, setting $_ respectively. comparable to a foreach loop so the above is more-or-less equivalent to#!/usr/bin/perl my $string = "Helloa, this is the string"; my @chars = qw(s,o,m,e,c,h,a,r,s); if ( grep $string =~ /$_/, @chars ) { print "OK"; } else { print "Oops"; }
hope this helpsforeach (@chars) { if ($string =~ /$_/) { $anymatches++ } } if ($anymatches) { print "OK" } else { print "Oops" }
p.s. just showing off but you could do this on one line: print grep $string=~/$_/,@chars ? "OK" : "Oops";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Some sexy regex
by Anonymous Monk on Mar 19, 2003 at 07:39 UTC |