Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Massive regexp search and replace

by grinder (Bishop)
on Feb 10, 2005 at 15:08 UTC ( [id://429761]=note: print w/replies, xml ) Need Help??


in reply to Massive regexp search and replace

Can anyone help me find an efficient implementation.

If the cost of a subroutine call is cheaper than scanning the list (which I suspect is the case), then you can assemble all the target patterns into one, perform a single match, and then dispatch to the sub that gives you what you want to substitute:

#! /usr/local/bin/perl -w use strict; use Regexp::Assemble; my $ra; my %dispatch = ( 'food' => sub { 'pizza' }, 'water' => sub { 'beer' }, 'like' => sub { 'enjoy' }, '(\d+)F' => sub { int(($ra->mvar(1) - 32 ) * 5/9 ) . 'C' }, ); $ra = Regexp::Assemble->new( track => 1 )->add( keys %dispatch ); while( <DATA> ) { while( $ra->match($_) ) { my $m = $ra->matched; s/$m/&{$dispatch{$m}}/e; } print; } __DATA__ I'd like a glass of water with my food, it's 92F in here!

... produces...

I'd enjoy a glass of beer with my pizza, it's 33C in here!

Generating the dispatch table from a data file is left as an exercise to the reader (but a pretty fun one, I might say).

- another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: Massive regexp search and replace
by albert.llorens (Initiate) on Feb 10, 2005 at 16:01 UTC
    Thanks grinder.

    I had already used module Regexp::Assemble for another quite similar script, and I had thought of using it with track=>1 for my replacement script. But I could not because my new script hanged whenever I created a Regexp::Assemble object with track=>1.

    The exact line in my code where the script hanged:
    my $Rpatt = Regexp::Assemble->new( chomp=>1, track=>1 )->add( keys(%PA +TTS) );
    Where %PATTS is a hash with elements of the form 'pattern->replacement'. Any idea why it hanged?
      Any idea why it hanged?

      Without seeing %PATTS, no idea. Maybe you have uncovered a bug! If you're interested in resolving the problem, I now have a mailing list for the module. If you care to join up we can maybe get to the bottom of the problem.

      - another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2025-07-13 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.