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

to how may concern: I need a little help with replacement for more then one character in one single string. for example the string is "Description=Home Page for the..." and I'd like to catch find the word "Description" and replace with "Summary", find "=" and replace with ":", and also the word of "Description" is possible in all lowercase or uppercase. I have been trying this way, but it does not seem to work.
my @find = ('description', 'Description', 'DESCRIPTION'); my $replace = ('Summary'); @find = quotemeta @find; $newString =~ s/@find/$replace/g;
help please

Replies are listed 'Best First'.
Re: more then one string replacement
by MZSanford (Curate) on Jan 07, 2002 at 22:20 UTC
    You can use the i modifier to make it case insensitive ... like :

    $string =~ s/^description=/Summary:/gi;

    $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
Re: more then one string replacement
by andye (Curate) on Jan 07, 2002 at 22:37 UTC
    Here's one way:

    $string =~ s/description/summary/gi ; $string =~ tr/=/:/ ;
    and here's another:
    %subs = ( description => 'Summary', '=' => ':', blah => 'wibble' ); foreach (keys %subs) { $string =~ s/\Q$_/$subs{$_}/gi; }

    hth,
    andy.

(jeffa) Re: more then one string replacement
by jeffa (Bishop) on Jan 07, 2002 at 22:37 UTC
    Try this instead:
    use strict; my @find = ('description', 'Description', 'DESCRIPTION'); my $replace = 'Summary'; foreach my $string (@find) { $string = quotemeta $string; $string =~ s/description/$replace/ig; $string =~ s/=/:/g; }
    Since you are using an array, you will need to iterate through the array to make the replacements. I use two seperate regular expressions for two seperate replacements. I moved quotemeta to inside the loop, you can't call it on an array like you did - @find will contain one element that is the number of elements operated on by quotemeta. Now, you can chomp an array ... but you can't quotemeta it (not without a map or for loop at least).

    Also, you can't put an array in a regular expression:

    s/@array/replacement/
    Doesn't do what you think it does ;). Read more about regular expression at perlre. Brew up a pot of coffee and immerse yourself.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: more then one string replacement
by dmmiller2k (Chaplain) on Jan 08, 2002 at 01:32 UTC

    What you are trying to do with this line:

    s/@find/$replace/g;

    is not, strictly speaking, possible in the way you meant it. Others here have suggested iterating through your @find array, searching for each element. In this case, you'd be better off simply using a case-insensitive search (as recommended by others), and not enumerating all of the expected case variations on the word, 'description'.

    However, since regexes are interpolated as double-quoted strings, you may say something like:

    { # $" is the separator Perl uses to join array elements within a # double-quoted string, in effect, as the value of join($", @find) local $" = '|'; s/@find/$replace/g; tr/=/:/; }

    Although, in this case, you really would be better off with: s/description/Summary/gi; followed by tr/=/:/;.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime