Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

matching an array of alternates

by Anonymous Monk
on Jun 23, 2000 at 23:12 UTC ( #19644=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have an array @latin_am=("Ecuador","Mexico");
The actual array contains 10 countries.
I want to scan a text file for any line that matches /Ecuador/ or /Mexico/ or or any other country in the array.
What is the most efficient way of accomplishing this? Thanks very much.

Comment on matching an array of alternates
Re: matching an array of alternates
by btrott (Parson) on Jun 23, 2000 at 23:17 UTC
    Build a regular expression that will match those countries, then scan through the file and check each line against it:
    my $regex = join '|', @latin_am; while (<>) { print $1 if /$regex/; }
    This reads from STDIN, so invoke it like this:
    % foo.pl file
      Just wanted to point out that if the file you're searching has a lot of lines, you can gain some speed by adding the "o" option to the regex; i.e.:
      print $1 if /$regex/o;
      This way, the regular expression only has to be compiled once.

      - Zoogie

Re: matching an array of alternates
by perlmonkey (Hermit) on Jun 24, 2000 at 02:27 UTC
    This should do the trick:
    @latin_am=("Ecuador","Mexico"); $regex = join ("|", @latin_am); open( FILE, 'input.txt' ) or die "input.txt: $!"; @lines = grep { /$regex/o } <FILE>;
    @lines will hold all the lines of the file 'input.txt' that match any of the phrases in @latin_am
Re: matching an array of alternates
by takshaka (Friar) on Jun 24, 2000 at 07:23 UTC
    The pipe-joined monolithic regex isn't terribly efficient.

    The qr// approach described in perlfaq6 (that is, versions of perlfaq6 that ship with Perl 5.005 or later, but not the one on this site) is much faster.

    my @regexes = map { qr/\Q$_/ } @latin_am; while (<>) { for my $regex (@regexes) { if ($_ =~ $regex) { print; last; } } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-10-23 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls