in reply to grepping a large file and stopping on first match to a list
Does this stop searching on the first match?Well, it does, you can test it yourself:
a more interesting question is: does the expression $map =~ m"^(.*)$"gm put 861_600 lines on perl's argument stack before even calling first? It does, which IMO kind of defeats the purpose of "stopping on the first match".$ perl -le ' use List::Util "first"; first { $_->() } sub { print 0; 0 }, sub { print 1; 1 }, sub { print 2; 1 }; '
The approach suggested by kcott (compiling the combined regex; not the tie stuff) should be fairly efficient. I use it often. I also recommend to avoid smartmatch; it's too smart for most programmers ("27-way recursive runtime dispatch by operand type" is something I personally don't even want to understand). If by "exactly matches" you mean literally matches (as by eq), compile regex like this:
and use it like this:my $regex = join '|', map quotemeta, @strings; $regex = qr/^($regex)$/;
I don't see much point in tie'ing the file; nor in memory mapping it, for that matter. But maybe you need the map for something else. If not, just read the file in the usual way, using the while loop.$string = $1 if $map =~ $regex;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: grepping a large file and stopping on first match to a list
by msh210 (Monk) on Feb 23, 2016 at 07:15 UTC | |
by Athanasius (Archbishop) on Feb 23, 2016 at 07:57 UTC | |
by msh210 (Monk) on Feb 23, 2016 at 15:32 UTC | |
by Anonymous Monk on Feb 23, 2016 at 08:01 UTC | |
by kcott (Archbishop) on Mar 23, 2016 at 10:34 UTC |