Edward Wijaya wrote:
> Hi,
> 
> Suppose I have a data file that contain these lines:
> output1
> output2
> 
> when I run the code below
> with: perl mycode.pl -f datafile
> it gives:
> 
> Trial 1
> output1
> output2
> Trial 2
> 
> 
> instead of:
> 
> Trial 1
> output1
> output2
> Trial 2
> output1
> output2
> 
> Can we actually loop over the 'while diamond'?
> Please kindly advice how can I overcome this problem.
> 
> Thanks so much for your time.
> 
> Regards,
> Edward WIJAYA
> SINGAPORE
> 
> 
> __BEGIN__
> use strict;
> use warnings;
> use Getopt::Std;
> 
> our $opt_f;
> getopts('f:');
> 
> open INFILE, "<$opt_f" or die "$0:  Can't open file $opt_f: $!";
> 
> my $trial = 2;
> 
> #What's wrong with running a for loop over the 'while' here?
> for ( my $t = 1 ; $t <= $trial ; $t++ ) {
>      print "Trial ", $t, "\n";
> 
>      while (<INFILE>) {
>          print;
>      }
> }

When you reach the end of file on INFILE (during Trial 1), further reads
will just return undef. You need to either close and reopen the file, or
rewind the file using seek() before you can re-read the data.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to