Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Replace after match in regex (key value subsitution)

by ikegami (Patriarch)
on Oct 23, 2008 at 04:28 UTC ( [id://718945]=note: print w/replies, xml ) Need Help??


in reply to Re: Replace after match in regex (key value subsitution)
in thread Replace after match in regex (key value subsitution)

Almost.
  • You forgot to convert the text to a regexp pattern.
  • You only replace the first instance.
  • And since I'm already changing the line, I'll remove the useless use of map.
$text =~ s/\Q$_\E/$fixers{$_}/g for keys %fixers;

It could still be improved if it's going to be done repeatedly.

my ($re) = map qr/$_/, join '|', map quotemeta, keys %fixers; while (...) { ... $text =~ s/$re/$fixers{$_}/g for keys %fixers; ... }

Or even better for pre-5.10

use List::Regexp qw( ); my $re = List::Regexp->new()->list2re( keys %fixers ); while (...) { ... $text =~ s/$re/$fixers{$_}/g for keys %fixers; ... }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-25 00:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found