crickson has asked for the wisdom of the Perl Monks concerning the following question:

This is probably a very simple question, but it is annoying me.

Code fragment:

my $score=100000; open (IN, $arq); while (<IN>) { open (IN1, $_); while (<IN1>) { my @temp=split(/\t/); if ($temp[1]<$score) {

When executing this code, it complains "use of unitialized value $score in numeric lt...".

I cannot figure out why the variable, defined in the top most level of the program is not the same in the IF condition.

TIA

Replies are listed 'Best First'.
Re: Variable scope issue
by jwkrahn (Abbot) on Jun 26, 2019 at 19:12 UTC

    You didn't verify that either open succeeded. You didn't chomp the value you are using as a file name so it probably failed!

    my $score = 100_000; open my $IN, '<', $arq or die "Cannot open '$arq' because: $!"; while ( <$IN> ) { chomp; open my $IN1, '<', $_ or die "Cannot open '$_' because: $!"; while ( <$IN1> ) { my @temp = split /\t/; if ( $temp[ 1 ] < $score ) {
      You didn't verify that either open succeeded. You didn't chomp the value you are using as a file name...

      Very good points!

      ... so it probably failed!

      Sorry, no - open: "The filename passed to the one- and two-argument forms of open will have leading and trailing whitespace deleted and normal redirection characters honored."

      Even if the if the second open failed, it doesn't explain the warning, as the inner while loop would not execute - the same comment applies to LanX's post.

Re: Variable scope issue
by BillKSmith (Monsignor) on Jun 27, 2019 at 15:27 UTC
    There does not seem to be anything wrong with the code you posted. I understand it can be difficult to write an SSCCE when you do not know what is wrong. Perhaps this will help. As you can see, the following code does what I think you want without error. Please modify it to demonstrate your error. Post the failing code. Remember, we have to be able to run it and reproduce your error message.
    >type crickson.pl use strict; use warnings; my $arg = \'$infile1'; my $infile1 = \"one_score\t999"; my $score=100000; open my $IN, '<', $arg or die "error opening list of files"; while (<$IN>) { $_ = eval $_; # Bad practice - Use only for 'in-memory' file. open my $IN1, '<', $_ or die "error opening file $_"; while (local $_ = <$IN1>) { my @temp = split /\t/; if ($temp[1] < $score) { warn "\$temp[1] not defined\n" if !defined $temp[1]; warn "\$score not defined\n" if !defined $score; print "$temp[1] < $score\n"; } } } >perl crickson.pl 999 < 100000 >
    Bill
Re: Variable scope issue
by LanX (Saint) on Jun 26, 2019 at 17:36 UTC
    A guess: I think it's @temp which is undefined because you're messing up $_ twice and your second open fails.

    Can't test now...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      The second open works and it is not @temp the issue, as I have verified both.

      It is really $score, as its previous initialization is not considered inside the IF.

        as I have verified both

        How? Unless there is a substantial bug in the version of Perl you are using, your assertion "as its previous initialization is not considered inside the IF" is wrong.

        What version of Perl are you using and on what platform?

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Variable scope issue
by Anonymous Monk on Jun 26, 2019 at 17:37 UTC
    did you check $temp1?

      The temp1 was an issue with the HTML format. I have corrected that.