Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: regex with arrays and variables

by Random_Walk (Prior)
on Apr 26, 2013 at 08:18 UTC ( #1030789=note: print w/replies, xml ) Need Help??


in reply to regex with arrays and variables

If you have a lot of regex to use, and want to know which one matched, you can speed things up by pre-compiling them.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = ("foo {abc123}\n", "bar {def456}\n", "baz {ghi789}\n"); my @patterns = ("foo", "bar", "quux"); # pre compile regex my %regex; for (@patterns) { $regex{$_}=qr/$_/; } for my $i (@array) { print "checking $i"; # was chomping then adding a \n" for (keys %regex) { if ($i =~ $regex{$_}) { print "$i is in patterns (matches $_) - skipping\n"; last; } } print "$i IS NOT IN PATTERNS\n"; # Do something with $i now. }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030789]
help
Chatterbox?
[erix]: yeah. it's been some time since I last built a (linux) kernel but perl takes less than 30s and postgresql less than 2 minutes minutes :)
[talexb]: Yep .. came across SuSE 6.2 in my storage locker recently. How old is that.
[erix]: 2001 :)
[erix]: (I have the wp pages open :))

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2018-07-17 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (374 votes). Check out past polls.

    Notices?