marcpascual has asked for the wisdom of the Perl Monks concerning the following question:
I just learned that this won't work because the back reference only works from the regex operator, and what I'm doing on the hash values down here is being taken literally.
My question is, what techniques can I look into in order to achieve the desired result?
Eventually, I would like to put the hash on an external text file with pattern on the left side, including grouping, a delimeter, and the replacement in the right side, including back references.
#!/usr/bin/perl use strict; my %fill = ( #'M(ary)' => 'G\1', 'M(ary)' => 'G$1', ); while (my $line = (<DATA>)) { foreach my $key (keys %fill) { if ($line =~ /$key/) { $line =~ s/$key/$fill{$key}/; } } print "$line"; } __DATA__ Mary has a little lamb
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: backreference on hash
by AnomalousMonk (Archbishop) on Aug 13, 2015 at 22:40 UTC | |
|
Re: backreference on hash
by stevieb (Canon) on Aug 13, 2015 at 22:44 UTC | |
|
Re: backreference on hash
by Eily (Monsignor) on Aug 13, 2015 at 22:29 UTC | |
|
Re: backreference on hash
by MidLifeXis (Monsignor) on Aug 14, 2015 at 13:24 UTC |