in reply to problem with substitute in regexp
Here's an example using the keys of the hash to generate the substitution search regex.
>perl -wMstrict -le "my %replace = ( '111' => 'bar', '1111' => 'world', '333' => 'perl', ); my $find = join '|', reverse sort keys %replace; ;; my $str = 'f111,f1111,f333'; $str =~ s{ f ($find) }{$replace{$1}}xmsg; print qq{'$str'}; " 'bar,world,perl'
upaksh: For extra credit (after you've perused Markup in the Monastery and Writeup Formatting Tips for penance), why is the reverse sort necessary in generating the substitution search regex for the example replacement mapping given? What happens if the reverse is left out of the sort? (Well, it "doesn't work right", but why?)
|
|---|