
japhy
Enthusiast
Mar 10, 2000, 3:41 AM
Post #2 of 6
(343 views)
|
The position of your loops is what's causing you problems. And, for one thing, $start never gets set back to 0 (so it's never less than 3 after the first line is played with). Also, the ORDER of your loops is wrong for the logic you want. You're currently doing: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> while (<FILE> ) { for (0 .. 2) { # change line } } </pre><HR></BLOCKQUOTE> ... but the output you ask for means you want this order: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> for (0 .. 2) { while (<FILE> ) { # change line } } </pre><HR></BLOCKQUOTE> Applying this, we fix your program: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> # after setting vars and opening the file... for $i (0,1,2) { # or: for $i (0 .. 2) { while (<FILE> ) { # the line is held in $_ s/fruit/$fruits[$i]/g; # operates on $_ print; # defaults to printing $_ } } </pre><HR></BLOCKQUOTE>
|