scottdware has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to search and replace values that are stored in an array. Such as PHP's preg_replace function. I want to have an array of values like: @search = ("red", "orange", "blue"); @replace = ("yellow", "black", "green"); and be able to =~ s/$search/$replace/ig. Is this possible? Thanks!

Replies are listed 'Best First'.
Re: search and replace from array values
by davidrw (Prior) on Dec 06, 2005 at 13:29 UTC
    You can build a regex with the @search array, and substitute in the @replace value via a hash lookup.
    my $s = "I have red stuff, blue stuff, orange stuff, and more red stuf +f."; my @search = ("red", "orange", "blue"); my @replace = ("yellow", "black", "green"); my %replacements; @replacements{ @search } = @replace; # use a hash slice to populate + the search=>replace pairs. my $re = join '|', @search; $s =~ s/\b($re)\b/$replacements{$1}/g; print $s;
Re: search and replace from array values
by jhourcle (Prior) on Dec 06, 2005 at 13:44 UTC
Re: search and replace from array values
by Tomte (Priest) on Dec 06, 2005 at 13:36 UTC

    hmm,

    @search = ("red", "orange", "blue"); @replace = ("yellow", "black", "green"); $text = "my pig is red, my orange is orange and the sky is blue"; foreach (0..$#search) { $text =~ s/$search[$_]/$replace[$_]/; } print $text,"\n" __END__ my pig is yellow, my black is orange and the sky is green
    To replace every ocasion in text, use the g-modifier (s///g)

    not a beauty, but works...

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: search and replace from array values
by wfsp (Abbot) on Dec 06, 2005 at 13:48 UTC
    Here's my go. Similar to davidrw's but uses the e modifier (treats the right hand side as an expression).

    #!/bin/perl5 use strict; use warnings; my @search = ("red", "orange", "blue"); my @replace = ("yellow", "black", "green"); my %replacements; @replacements{ @search } = @replace; my $string = q(red orange blue blue); for my $key (keys %replacements){ $string =~ s/$key/$replacements{$key}/ge; }
      The major difference is that your loop makes multiple passes through the string, and davidrw's does not. Depending on the size of the string and how many search terms there are, the performance difference can be quite significant.

      Caution: Contents may have been coded under pressure.
Re: search and replace from array values
by blazar (Canon) on Dec 06, 2005 at 14:16 UTC
    You may want to (do something like)
    s/\b(\w+)\b/ $subst{$1} || $1/ge;
    that is, you may want to do a more accurate check if %subst can contain, say 0 or '' as a value. Now, how to build the %subst associative array from your lists/arrays? That is left as an exercise to the reader => see perldoc perlsyn.
Re: search and replace from array values
by svenXY (Deacon) on Dec 06, 2005 at 13:30 UTC
    Hi,
    you mean that you want to replace the first element in the first array with the first element in the second array and the second with the second and so on?
    What have you tried so far?
    Regards,
    svenXY