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

Re: regex with arrays and variables

by Random_Walk (Parson)
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!


Comment on Re: regex with arrays and variables
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-07-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls