in reply to How to restart a while loop that is iterating through each line of a file?

If the matter is just 16 lines from a file then read it in memory (an array, for example), like:

# untested open my $read_fh, '<', 'source.txt' or die "$!"; my @lines; while (push @lines,<$read_fh>){ last if $. == 16; } foreach my $count (0..33){ open my $out, '>', 'afilename'.$count,'.log' or die "$! writing"; print $out $_ for @lines; close $out; }

But in the case you really need to restart a loop over a file read you can use seek against the filehandle.

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re: How to restart a while loop that is iterating through each line of a file?
  • Download Code