in reply to Re: advice for reading data from a file
in thread advice for reading data from a file

Hi Roger,

i'm using 'warnings', but thanks for the 'defined' that i've missed :}

but plz can you explain me why you are checking $#fields again in the line:

return 0 if $#fields < 0;

as it's already done in the loop (==3) ??

Replies are listed 'Best First'.
Re: Re: Re: advice for reading data from a file
by Roger (Parson) on Jan 19, 2004 at 00:51 UTC
    That will guard against the case when your file has less than 2 lines, and the @fields only gets populated by the second line of the file.

      thanks for the explanation Roger,

      but concerning the 'defined' in the loop my code break breaks with this error:

      Use of uninitialized value in scalar chomp at /data/tmp/test_check_fie +ld.pl line 31, <$fh> line 2. <br><br> Use of uninitialized value in split at /data/tmp/test_check_field.pl l +ine 32, <$fh> line 2. <br> *** ERROR number of fields !=4
      after reading perldoc 'defined' :

      defined Returns a Boolean value telling whether EXPR has a value other than the undefined value "undef". If EXPR is not present, $_ will be checked.

      it seems to me that it's normal that my code break as it's not the value of the line that is returned but instead the boolean value of 'defined'..or am i wrong somewhere ?
        My mistake, should be while (defined ($_ = <$fh>)) {.... Forgot the bracket arround <$fh>. graff suggests that the defined check is implicit, but I would still leave the code as it is. A bit of defensive programming would not hurt.