http://www.perlmonks.org?node_id=1030789


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!