arekfu has asked for the wisdom of the Perl Monks concerning the following question:
I have written a script that parses a file and applies a number of text substitutions. I have a dictionary (let's call it %patdic) of search/substitute pairs and, for each input text line, I run something like
foreach my $search (keys %patdic) { s/$search/$patdic{$search}/g; }
This works well as long as $patdic{$search} does not contain the $1 construct. Consider the following example:
#!/usr/bin/env perl use strict; use warnings; $_=q{nell'amaca}; my $search=q{nell'([[:alpha:]]+a)}; my $subst='na $1'; s/$search/$subst/g; print; print "\n";
This will return "na $1", while I would like it to return "na amaca". Any pointers, please?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Evaluating $1 construct in literal replacement expression
by balakrishnan (Monk) on Feb 06, 2009 at 14:48 UTC | |
|
Re: Evaluating $1 construct in literal replacement expression
by jwkrahn (Abbot) on Feb 06, 2009 at 12:16 UTC | |
by Anonymous Monk on Feb 06, 2009 at 13:04 UTC | |
|
Re: Evaluating $1 construct in literal replacement expression
by Anonymous Monk on Feb 06, 2009 at 11:57 UTC | |
by Anonymous Monk on Feb 06, 2009 at 11:58 UTC | |
by Anonymous Monk on Feb 06, 2009 at 12:10 UTC | |
|
Re: Evaluating $1 construct in literal replacement expression
by AnomalousMonk (Archbishop) on Feb 06, 2009 at 18:07 UTC | |
|
Re: Evaluating $1 construct in literal replacement expression
by gone2015 (Deacon) on Feb 06, 2009 at 16:22 UTC |