Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: getting error "Use of uninitialized value $lines in concatenation (.) or string at line 6"

by Athanasius (Archbishop)
on Jul 03, 2014 at 02:54 UTC ( [id://1092079]=note: print w/replies, xml ) Need Help??


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:

  1. my $lines++ while(<FILE>);

    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>;
  2. my $first = <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,

  • Comment on Re: getting error "Use of uninitialized value $lines in concatenation (.) or string at line 6"
  • Select or Download Code

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

    It works now. Thanks a lot for explanation and pointing out my mistake. I really appreciate it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1092079]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found