matching elements in a list in a logical OR fashion
Anonymous Monk
created: 2004-06-14 23:40:11
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?
Re: matching elements in a list in a logical OR fashion
created: 2004-06-14 23:46:42

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

Re^2: matching elements in a list in a logical OR fashion
created: 2004-06-15 00:03:03
This might be even faster, depending on what's in @array...
[cpan://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";
}
Re^2: matching elements in a list in a logical OR fashion
created: 2004-06-15 02:21:42
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

Re^2: matching elements in a list in a logical OR fashion
created: 2004-06-15 02:25:24
Thank for that, Zaxo.
That's really nice.

Re^2: matching elements in a list in a logical OR fashion
created: 2004-06-15 09:57:07

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
created: 2004-06-14 23:51:16
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";
}
Re^2: matching elements in a list in a logical OR fashion
created: 2004-06-15 00:04:15
thanks guys. brilliant.
Re: matching elements in a list in a logical OR fashion
created: 2004-06-15 00:37:54

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
created: 2004-06-15 10:19:08
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
ihb
created: 2004-06-15 20:31:43

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
created: 2004-06-16 11:21:57
my @array = qw( foo bar baz ); print grep(/^$bigstring$/, @array)? "match" : "no match","\n"; try that.

perlmonks.org content © perlmonks.org and Anonymous Monk, didier, hardburn, ihb, mako132, meetraz, NodeReaper, parv, Zaxo

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03