natxo has asked for the wisdom of the Perl Monks concerning the following question:
But if I print everything, rewind the file to the beginning, and print the first three lines again, it does not work:my $f = "/etc/fstab"; open ( my $fh, "<", $f ); while ( <$fh> ) { print if (1..3) ; }
using warnings, strict, autodie (implicit in the code).my $f = "/etc/fstab"; open ( my $fh, "<", $f ); while ( <$fh> ) { chomp; print "$_\n"; } print "=" x 78, "\n\n"; print "after 1st while loop\n"; seek( $fh, 0, 0); while ( <$fh> ) { print if (1..3) ; }
I can solve it using a counter inside the latest while loop, but I am just curious why it does not work as expected. Thanks for any insights.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: seek question
by toolic (Bishop) on Feb 09, 2017 at 20:56 UTC | |
by natxo (Scribe) on Feb 09, 2017 at 21:25 UTC | |
|
Re: seek question
by choroba (Cardinal) on Feb 09, 2017 at 21:02 UTC | |
by stevieb (Canon) on Feb 09, 2017 at 21:05 UTC | |
by Anonymous Monk on Feb 10, 2017 at 16:48 UTC | |
|
Re: seek question
by stevieb (Canon) on Feb 09, 2017 at 21:01 UTC | |
by kcott (Archbishop) on Feb 10, 2017 at 06:42 UTC | |
by Marshall (Canon) on Feb 09, 2017 at 21:34 UTC | |
by stevieb (Canon) on Feb 09, 2017 at 21:42 UTC |