Joseph Paish wrote:
how do you use the "line number" variable inside a while loop like i am trying to do below?

i never get to process the first line of the file, and as a result, subsequent calculations are inaccurate. it always jumps to the second "if" statement.

Like the others, I don't see any problems in the posted code other than maybe some stylistic differences. Here is how I would probably write it:


#!/usr/bin/perl

use strict;
use warnings;

use constant FILE => 'data';

open(my $fh, FILE )
    or die ("Can't open @{[FILE]}: $!") ;

while (defined( my $line = <$fh> )) {

    chomp( $line );

    if ( $. == 1 ) {
        # first line is initial values
        # process intial values here
        print "HEADER: $line\n\n";
    } else {
        #process subsequent values here
        print "> $line\n";
    }

} # end of while loop

close( $fh ) ;

__END__


Alternatively, you can use the range operator, which may be more idiomatic, in the condition:


    if ( 1 .. 1 ) {

 -or-

    unless ( 2 .. eof() ) {

See `perldoc perlop` - Range operator
Also, you may want to review the entire section in `perldoc perlvar` on the $. variable.


Randy.

--
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