>>>>> "JJ" == Jackie Jackie <j_jacki...@yahoo.ca> writes:


  JJ> I just want to replace the first occurrence of fish by red
  JJ> bird. The second occurrence by brown bear. etc.

then you should have stated this in the first posting. it was not clear
at all what you wanted.

  >> fish/red bird
  >> fish/brown bear
  >> fish/lion
  >> red fish/animal

note that that will fail because 'fish' is part of 'red fish' so you
have to do 'red fish' replacements first. you didn't specify that so i
won't consider it. you really need to learn how to make tighter
specifications.

<untested>

my %replace_values = (

        'fish'  => [ 'red bird', 'brown bear', 'lion' ],
        'red fish' => [ 'animal' ],
) ;

while( my ($key, $vals ) = each %replace_values ) {

        foreach my $replace_val ( @{$vals} ) {

                $text =~ s/$key/$replace_val/ ;
        }
}


if you were fine with running out of replacement values and substituting
a null string, then this works and is cute code. but it also destroys
the replacement value arrays.


        while( my ($key, $vals ) = each %replace_values ) {

                $text =~ s/$key/shift @{$vals}/e ;
        }

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to