Axlex has asked for the wisdom of the Perl Monks concerning the following question:
Hey wise monks,
as the title mentions, i'm trying to replace multiple strings in a string. i have 2 arrays one with the search strings and one with the replace strings. Now i wanna loop throug all search strings, but it doesnt work at all. :(
my example code so far:I would be grateful for any suggestions.use strict; use warnings; my $a = "aRep"; my $b = "bRep"; my $c = "cRep"; my @Search1 = ("?a?","?b?","?c?"); my @Replace = ($a,$b,$c); my $i=0; my $string = '?a? ?b? ?c?'; print "old string: $string1\n"; for (;$i<$#Search+1;$i++) { $string =~ s/"$Search[$i]"/"$Replace[$i]"/g; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Replace multiple strings in a string
by choroba (Cardinal) on May 07, 2014 at 15:10 UTC | |
by Axlex (Initiate) on May 07, 2014 at 15:34 UTC | |
|
Re: Replace multiple strings in a string
by InfiniteSilence (Curate) on May 07, 2014 at 15:51 UTC | |
|
Re: Replace multiple strings in a string
by Laurent_R (Canon) on May 07, 2014 at 17:50 UTC | |
by GrandFather (Saint) on May 08, 2014 at 10:54 UTC | |
by Laurent_R (Canon) on May 08, 2014 at 11:36 UTC | |
|
Re: Replace multiple strings in a string
by BillKSmith (Monsignor) on May 07, 2014 at 20:36 UTC | |
|
Re: Replace multiple strings in a string
by GrandFather (Saint) on May 08, 2014 at 11:12 UTC |