in reply to Replace multiple strings in a string
Focussing just on the "parallel" loops pattern (others have addressed why this is a poor solution to the actual problem), lets look at better solutions to managing related data. There are two common options in Perl for managing small amounts of related data depending on whether it is keyed or ordered.
If the data is keyed and the order doesn't matter then use a hash:
my %subs = ( '\?a\?' => 'aRep', '\?b\?' => 'bRep', '\?c\?' => 'cRep', ); for my $match (keys %subs) { $string =~ s/$key/$subs{Skey}/g; }
If the order is important then use an array:
my @subs = ( ['\?a\?' => 'aRep'], ['\?b\?' => 'bRep'], ['\?c\?' => 'cRep'], ); for my $match (@subs) { $string =~ s/$match->[0]/$match->[1]/g; }
In either case you no longer have to deal with indexing through parallel arrays so the loop code is clearer and there is no way you can get your parallel data out of sync.
|
|---|