in reply to Re: multi find/replace
in thread multi find/replace

Ah, beat me to the punch :) If you wanted to make sure you don't miss any matches in case you modify the hash in the future, you could make the following mods to Enlil's code:
... my $changeList = join('|',(keys %changes)); ... $string =~ s/($changeList)/$changes{$1}/g;


-jbWare

Replies are listed 'Best First'.
Re: Re: Re: multi find/replace
by mirod (Canon) on Feb 10, 2004 at 21:58 UTC

    In case you want the keys to include meta-characters that you don't want to treat as such, you might want to "protect" them:

    #!/usr/bin/perl -w use strict; my %changes=( foo => 'toto', bar => 'tata', 'fo*' => 'to*', ); my $changeList = join('|', map { "\Q$_\E"} keys %changes); my $string= "foo bar fo*"; $string =~ s/($changeList)/$changes{$1}/g; print $string;

    This way $changeList looks like (\Qkey1\E|\Qkey2|...), which instructs the regexp engine to treat everything between \Q and \E as a literal.