in reply to getting error "Use of uninitialized value $lines in concatenation (.) or string at line 6
Hello chiru, and welcome to the Monastery!
There are two major problems in your script:
This re-declares $lines on every iteration of the while loop. You need to declare it once only, before the loop:
my $lines; $lines++ while <FILE>;
At this point, the filehandle FILE points to the end of the file, because this is the position it reached at the conclusion of the previous while loop. Call seek to reset it:
seek FILE, 0, 0; my $first = <FILE>;
Note also that $ARGV has a special meaning in Perl (see perlvar#Variables-related-to-filehandles). You should use an ordinary lexical variable instead: chomp(my $file = <>);
And of course the design of this script is inefficient, since it reads the file twice. See NetWallah’s answer for a better, one-pass, approach.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: getting error "Use of uninitialized value $lines in concatenation (.) or string at line 6"
by chiru (Initiate) on Jul 03, 2014 at 05:26 UTC |