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

Also, you are performing a regex on the return value which may be undef.I would think you should do the regex before you return the field if (log that too)

excuse me but i don't understand, here i'm not returning the value of the field but the returning value of the regexp which i think can be only 0 or 1 but i maybe wrong.

do you mean that my sub can return 'undef' in some cases ?

  • Comment on Re: Re: Re: advice for reading data from a file

Replies are listed 'Best First'.
Re: Re: Re: Re: advice for reading data from a file
by JamesNC (Chaplain) on Jan 18, 2004 at 20:23 UTC
    Yes exactly. If the regex doesn't matchm $fields2 will be undef. Also, I think you need to put parenthesis around your ( split /;/ ) so that the context is the same (list context):
    use strict; my @fields; my $RS = ' '; my $rv = &do_this; print $rv; sub do_this { while(<DATA>){ next unless $. == 2; @fields = ( split /$RS/, $_ ), last if $. == 2; @fields = ( split /$RS/, $_ ), last if $. == 2; } return $fields[2] =~ /\d+/; } __DATA__ This is line one. I found 12 on line two. And This was found on line three.

    Change 12 on line to some text and try removing the parens around the split... and you will see the diffs.
    JamesNC