On Wednesday 02 Dec 2009 16:03:53 Jackie Jackie wrote:
> I have a repeated occurrence of a word that I want to replace by another
>  word.
> 
> _Data_
> fish
> fish
> fish
> fish
> 
> output
> fish/red fish
> fish/blue fish
> fish/yellow fish
> fish/red fish
> 
> I did not find a solution. Please help me.
> 

If I understand you correctly, you can use s/// with the /e flag. A tested 
example is:

<<<<<<<<<<<<<<<
#!/usr/bin/perl

use strict;
use warnings;

my $data = <<'EOF';
fish
fish
fish
fish
EOF

my @prefixes = (qw(red blue yellow));

my $curr_idx = 0;
sub get_new_word
{
    my $word = shift;

    return "$word/" . $prefixes[($curr_idx++)%3]. " $word";
}

my $new_data = $data;

$new_data =~ s/(fish)/get_new_word($1)/ge;

print $new_data;
>>>>>>>>>>>>>>>

> 
>       __________________________________________________________________
> The new Internet Explorer® 8 - Faster, safer, easier.  Optimized for Yahoo!
>   Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/
> 

And the obligatory http://getfirefox.com/ . ;-)

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Freecell Solver - http://fc-solve.berlios.de/

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.

--
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