Gunnar Hjalmarsson wrote:
Zembower, Kevin wrote:
I'm trying to write a program in which I have to make two passes through
the file. I want to call the file on the command line and process it
with 'while (<>){...'. Can I use something like 'seek STDIN, 0, 0'
between the two while loops to reset the diamond operator to the
beginning of the input file? I tried this but the program seemed to just
hang, waiting for input.

You can do it by reopening ARGV after the first iteration:

    my $file = $ARGV[0];
    print "First time: $_" while <>;
    open ARGV, $file or die $!;
    print "Second time: $_" while <>;

Or you could do it like this:

     my $file = $ARGV[0];
     print "First time: $_" while <>;
     @ARGV = $file;
     print "Second time: $_" while <>;

Or like this:

     $ARGV[1] = $ARGV[0];
     my $string = 'First time: ';
     while ( <> ) {
         print "$string$_";
         $string = 'Second time: ' if eof;
         }



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to