Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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 romping around the Monastery: (2)
As of 2015-07-06 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls