in reply to How to speed up multiple regex in a loop for a big data?
this way, the regular expresions are compiled just once, and also, the inner loop and the sort are removed from the while loop.open(MAP, "<$new_name_map_file"); while (<MAP>) { chomp; tr/A-Z/a-z/; @map_line = split (/\t/); $mapper{$map_line[0]} = $map_line[1]; } close(MAP); my $sub = "sub { "; for my $name (sort keys %mapper) { my $qname = quotemeta $name; my $qrepl = quotemeta $mapper{$name}; $sub .= "s{\b$qname\b}{$qrepl}g; "; } $sub .= "}"; $sub = eval $sub; die if $@; open(IN, "<input_file"); open(OUT, ">input_file.new"); while (<IN>) { print "%"; tr/A-Z/a-z/; $sub->(); print OUT "$_"; } close(IN); clse(OUT);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to speed up multiple regex in a loop for a big data?
by ruzam (Curate) on May 25, 2006 at 15:55 UTC | |
by salva (Canon) on May 25, 2006 at 16:36 UTC | |
by MonkInPleasanton (Initiate) on May 25, 2006 at 16:45 UTC | |
by salva (Canon) on May 25, 2006 at 16:58 UTC | |
by ruzam (Curate) on May 25, 2006 at 17:10 UTC |