in reply to Search and replace text

Like this?

my $num; while (<FILE>) { chomp; if ($. % 3 == 1) { $num = sprintf("%02d", rand(100)); substr($_, 0, 2, $num); } else { substr($_, 1, 2, $num); substr($_, -2, 2, sprintf("%02d", rand(100))); } print("$_\n"); }

If the line terminator is one character long, the above is simplifies to:

my $num; while (<FILE>) { if ($. % 3 == 1) { $num = sprintf("%02d", rand(100)); substr($_, 0, 2, $num); } else { substr($_, 1, 2, $num); substr($_, -3, 2, sprintf("%02d", rand(100))); } print; }

Replies are listed 'Best First'.
Re^2: Search and replace text
by nsyed (Novice) on Jan 27, 2006 at 04:00 UTC
    Thanks everyone. I have another related question. Is it possible to randomly generate char within a certain range? For example if i want to generate two random chars between "aa" and "ff" so that the output will be like
    ab ad df ca . . .
    PS. this is not a homework assignment. I finished college long ago. I just learn new tools to help me do my job and Perl is the mother of all tools.