silentius has asked for the wisdom of the Perl Monks concerning the following question:
Revered Monks,
For each input string $str I have to test for several possible substitutions, say:
if $str contains tag1 then replace tag1 (in $str) by subst1;
if $str contains tag2 then replace tag2 by subst2;
and so on, and I have 49 such (tag, subst) pairs.
My question, dear and revered and full of wisdom monks, is: could you enlighten me as to a "good" way of programming this?
I mean, do I have any better choice than writing 49 lines like these?:
$str =~ s/tag1/subst1/; $str =~ s/tag2/subst2/; $str =~ s/tag3/subst3/; ... $str =~ s/tag49/subst49/;
At least, is there any syntactic sugar for those 49 lines?
Humbly and in much appraisal,
silentius
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: reg. expr. multiple substitutions
by BrowserUk (Patriarch) on May 17, 2012 at 13:26 UTC | |
by kennethk (Abbot) on May 17, 2012 at 14:34 UTC | |
by BrowserUk (Patriarch) on May 17, 2012 at 15:06 UTC | |
by kennethk (Abbot) on May 17, 2012 at 15:24 UTC | |
by BrowserUk (Patriarch) on May 17, 2012 at 15:46 UTC | |
| |
by mbethke (Hermit) on May 17, 2012 at 15:38 UTC | |
by BrowserUk (Patriarch) on May 17, 2012 at 15:41 UTC | |
|
Re: reg. expr. multiple substitutions
by Eliya (Vicar) on May 17, 2012 at 14:06 UTC | |
|
Re: reg. expr. multiple substitutions
by dulwar (Monk) on May 17, 2012 at 16:53 UTC | |
|
Re: reg. expr. multiple substitutions
by silentius (Monk) on May 17, 2012 at 18:18 UTC |