Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

matching elements in a list in a logical OR fashion

by Anonymous Monk
on Jun 15, 2004 at 03:40 UTC ( #366756=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I searched for this, but I could not, for the life of me, think of a concise way to do it, and thus get an answer without asking.

Lets say I have: @array(foo, bar, baz).
and I want to match those items in an or fashion.
thusly: if($bigstring =~ /(foo|bar|baz)/i){blah}
except instead of typing out foo, bar, and baz I would just hit the array instead.

Any hints?

Comment on matching elements in a list in a logical OR fashion
Re: matching elements in a list in a logical OR fashion
by Zaxo (Archbishop) on Jun 15, 2004 at 03:46 UTC

    This is one of my favorite tricks,

    my $re = do { local $" = '|'; qr/@array/; }; if ($bigstring =~ /$re/ ) { #... }
    The customary way is to join '|', @array in constructing the regex.

    If @array has regex metacharacters, it may be needful to escape them:

    my $re = do { local $" = '|'; qr/@{[map { quotemeta } @array]}/; };

    After Compline,
    Zaxo

      This might be even faster, depending on what's in @array...
      Regex::Presuf works wonders, and /o usually helps too.

      use strict; use Regex::PreSuf; my @array = qw( foo bar baz ); my $re = presuf(@array); my $bigstring = 'whatever'; if ($bigstring =~ /$re/o) { print "it matched"; }
      This node was taken out by the NodeReaper on Tue Jun 15 06:01:16 2004 (EST)
      Reason: valdez delete, empty node

      For more information on this node visit: this

      Thank for that, Zaxo.
      That's really nice.

      For the metacharacters case, how about this:

      my $re = do { local $" = '\E|\Q'; qr/\Q@array\E/; };

      That lets you ditch the map and array refernceing/dereferencing inside the regex. Though it is arguably not as clear.

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: matching elements in a list in a logical OR fashion
by meetraz (Hermit) on Jun 15, 2004 at 03:51 UTC
    Here's another way:
    use strict; use Regexp::Match::Any; my @array = qw(Foo Bar Baz); my $bigstring = 'whatever'; if($bigstring =~ match_any(\@array)){ print "It matched\n"; }else{ print "It didn't match\n"; }
      thanks guys. brilliant.
Re: matching elements in a list in a logical OR fashion
by parv (Priest) on Jun 15, 2004 at 04:37 UTC

    I have something like the following in one of my own modules ...

    print match_one( "string" , [ qw/character word paragraph string/ ] ) ? "matched" : "no match" ; sub match_one { my ($string , $list) = @_; foreach ( @{$list} ) { return 1 if $string =~ m/$_/; } return; }
Re: matching elements in a list in a logical OR fashion
by mako132 (Initiate) on Jun 15, 2004 at 14:19 UTC
    It'll be in Perl6 and is available now in Perl6::Rules. From the perldoc:

    An interpolated array:

    use Perl6::Rules; @cmds = ('get','put','save','load','dump','quit'); $str =~ m/ @::cmds /;
    matches if any of its elements eq matches the string at that point. So the above example is equivalent to:
    $str =~ /get|put|save|load|dump|quit/;
Re: matching elements in a list in a logical OR fashion
by ihb (Deacon) on Jun 16, 2004 at 00:31 UTC

    Using an joined pattern like "foo|bar|baz" can be quite inefficient depending on the patterns, since the regex engine can't find "anchored substrings" and can't guess where the match is. So here's some more or less attractive alternatives.

    The first one is the one I prefer if I don't know anything about the string nor patterns since it doesn't do more matches than necessary (comparing with the other two) and need it as an expression. It utilizes an "inline sub" as I call them, I don't know if there's any other name for them. It's a closure and why I use a subroutine for this is that I want to use it as an expression and be able to jump out of it. So &{sub { ... }} is like do { ... } except you can use return in it. (Note the subtle difference between &{sub { ... }} and sub { ... }->().) I admit it is a bit ugly, but at the same time I like it.

    if (&{sub { $bigstring =~ /$_/ and return 1 for @array }}) { ...; }

    Of course, a less hacky way could be

    my $matched; $matched = $bigstring =~ /$_/ and last for @array; if ($matched) { ...; }

    which I prefer even more if I don't need it as an expression.

    This next one counts the matches, which is inefficient if you just want one match.

    if (grep $_, map $bigstring =~ /$_/, @array) { ...; }

    This one below does essentially the same, but is less memory hungry for big lists.

    if (map $bigstring =~ /$_/ ? 1 : (), @array) { ...; }

    I'm not sure I really helped here...
    ihb

Re: matching elements in a list in a logical OR fashion
by Anonymous Monk on Jun 16, 2004 at 15:21 UTC
    my @array = qw( foo bar baz ); print grep(/^$bigstring$/, @array)? "match" : "no match","\n"; try that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://366756]
Approved by meetraz
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-09-20 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (155 votes), past polls