variable bewilderingly becomes undefined

Discussion in 'Perl Misc' started by Taylor Venable, Oct 17, 2007.

  1. Hello all,

    I have some text in a file, that I'm scanning over and pulling out of
    columns for use in some graphs. One piece of the code, however, is
    bothering me because a variable seems to suddenly become undefined for no
    reason. Here's the code:

    my $i = 0;
    385: while ($i < $timeMax + .01) {
    print "DOOD - timeMax undef!\n" if (not defined $timeMax);
    print "$i:$timeMax";
    unshift @xs, $i;
    unshift @ys, scalar (grep { $_ > $i && $_ < $i + 0.01 } @times);
    390: $i += .01;
    }

    This is for a histogram graph, so I'm finding the number of entries in the
    data set that fall in the current range, starting at zero and going up
    until the maximum. I know it's not perfect, but for now I'd settle for any
    results period, because at the moment the output is:

    0.35:0.380084
    0.36:0.380084
    0.37:0.380084
    0.38:0.380084
    0.39:0.380084
    Use of uninitialized value in addition (+) at ./stress.pl line 385,
    <$opt{...}> line 20.
    DOOD - timeMax undef!
    Use of uninitialized value in concatenation (.) or string at ./stress.pl
    line 387, <$opt{...}> line 20.
    0:Use of uninitialized value in addition (+) at ./stress.pl line 390,
    <$opt{...}> line 20.

    So it seems that $timeMax is suddenly undef'd while inside the loop. How
    can this be? The numbers I'm pulling from the file are at the end of the
    line and haven't been chomp()'d, but I wouldn't think that should matter.

    Great thanks for any help.

    Taylor Venable
    Taylor Venable, Oct 17, 2007
    #1
    1. Advertising

  2. On Oct 17, 9:54 am, Taylor Venable <> wrote:
    > Hello all,
    >
    > I have some text in a file, that I'm scanning over and pulling out of
    > columns for use in some graphs. One piece of the code, however, is
    > bothering me because a variable seems to suddenly become undefined for no
    > reason. Here's the code:
    >
    > my $i = 0;
    > 385: while ($i < $timeMax + .01) {
    > print "DOOD - timeMax undef!\n" if (not defined $timeMax);
    > print "$i:$timeMax";
    > unshift @xs, $i;
    > unshift @ys, scalar (grep { $_ > $i && $_ < $i + 0.01 } @times);
    > 390: $i += .01;
    > }
    >
    > This is for a histogram graph, so I'm finding the number of entries in the
    > data set that fall in the current range, starting at zero and going up
    > until the maximum. I know it's not perfect, but for now I'd settle for any
    > results period, because at the moment the output is:
    >
    > 0.35:0.380084
    > 0.36:0.380084
    > 0.37:0.380084
    > 0.38:0.380084
    > 0.39:0.380084
    > Use of uninitialized value in addition (+) at ./stress.pl line 385,
    > <$opt{...}> line 20.
    > DOOD - timeMax undef!
    > Use of uninitialized value in concatenation (.) or string at ./stress.pl
    > line 387, <$opt{...}> line 20.
    > 0:Use of uninitialized value in addition (+) at ./stress.pl line 390,
    > <$opt{...}> line 20.
    >
    > So it seems that $timeMax is suddenly undef'd while inside the loop. How
    > can this be? The numbers I'm pulling from the file are at the end of the
    > line and haven't been chomp()'d, but I wouldn't think that should matter.



    perhaps $timeMax is a global variable that gets undefined somewhere
    else? i understand that you want to focus on the block of code where
    the problem is manifesting itself, but in this case I would think that
    any code that references $timeMax could be relevant.
    it_says_BALLS_on_your forehead, Oct 17, 2007
    #2
    1. Advertising

  3. it_says_BALLS_on_your forehead wrote:

    > perhaps $timeMax is a global variable that gets undefined somewhere
    > else? i understand that you want to focus on the block of code where
    > the problem is manifesting itself, but in this case I would think that
    > any code that references $timeMax could be relevant.


    That is one of the things that confounds me: it is local to the subroutine
    in which this all occurs (declared with "my"), it is only assigned once,
    and it is not a reference. The value is the result of a "max" function,
    defined as:

    sub max($) {
    my $list = shift;
    my $max = $$list[0];
    foreach my $elt (@$list) {
    $max = $elt if ($elt > $max);
    }
    return $max;
    }

    The definition of timeMax is:

    my $timeMax = max(\@timing); # where @timing contains the timing data

    So I don't think this should be making timeMax a refernce, should it? And
    although a lot of the code is parallelized, this part is only executed once
    per program run.
    Taylor Venable, Oct 17, 2007
    #3
  4. On Oct 17, 10:26 am, Taylor Venable <> wrote:
    > it_says_BALLS_on_your forehead wrote:
    > > perhaps $timeMax is a global variable that gets undefined somewhere
    > > else? i understand that you want to focus on the block of code where
    > > the problem is manifesting itself, but in this case I would think that
    > > any code that references $timeMax could be relevant.

    >
    > That is one of the things that confounds me: it is local to the subroutine
    > in which this all occurs (declared with "my"), it is only assigned once,
    > and it is not a reference. The value is the result of a "max" function,
    > defined as:
    >
    > sub max($) {
    > my $list = shift;
    > my $max = $$list[0];
    > foreach my $elt (@$list) {
    > $max = $elt if ($elt > $max);
    > }
    > return $max;
    >
    > }
    >
    > The definition of timeMax is:
    >
    > my $timeMax = max(\@timing); # where @timing contains the timing data
    >
    > So I don't think this should be making timeMax a refernce, should it? And
    > although a lot of the code is parallelized, this part is only executed once
    > per program run.


    what happens if you print Dumper( \@timing ) before and after each
    time your max sub is called?
    it_says_BALLS_on_your forehead, Oct 17, 2007
    #4
  5. Taylor Venable

    Mumia W. Guest

    On 10/17/2007 09:26 AM, Taylor Venable wrote:
    > [...]
    > The definition of timeMax is:
    >
    > my $timeMax = max(\@timing); # where @timing contains the timing data
    >
    > So I don't think this should be making timeMax a refernce, should it? And
    > although a lot of the code is parallelized, this part is only executed once
    > per program run.


    What do you mean by parallelized?
    Mumia W., Oct 17, 2007
    #5
  6. On Oct 17, 3:26 pm, Taylor Venable <> wrote:
    > a "max" function,
    > defined as:
    >
    > sub max($) {


    Any reason you don't use the standard List::Utils::max()?
    Brian McCauley, Oct 17, 2007
    #6
  7. On Oct 17, 2:54 pm, Taylor Venable <> wrote:

    > So it seems that $timeMax is suddenly undef'd while inside the loop.


    What reason do you have to suspect it was ever defined?

    Please see the posting guidelines for this group.
    Brian McCauley, Oct 17, 2007
    #7
  8. On Oct 17, 3:26 pm, Taylor Venable <> wrote:

    > That is one of the things that confounds me: it is local to the subroutine
    > in which this all occurs (declared with "my"), it is only assigned once,
    > and it is not a reference. The value is the result of a "max" function,
    > defined as:
    >
    > sub max($) {
    > my $list = shift;
    > my $max = $$list[0];
    > foreach my $elt (@$list) {
    > $max = $elt if ($elt > $max);
    > }
    > return $max;
    >
    > }
    >
    > The definition of timeMax is:
    >
    > my $timeMax = max(\@timing); # where @timing contains the timing data
    >
    > So I don't think this should be making timeMax a refernce, should it?


    What do references have to do with the price of fish?

    $timeMax is undef. This will happen if @timing is empty (or indeed if
    one element of @timing is undef and all the rest are negative, or...).
    Brian McCauley, Oct 17, 2007
    #8
  9. Taylor Venable <> writes:

    > Hello all,
    >
    > I have some text in a file, that I'm scanning over and pulling out of
    > columns for use in some graphs. One piece of the code, however, is
    > bothering me because a variable seems to suddenly become undefined for no
    > reason. Here's the code:
    >
    > my $i = 0;
    > 385: while ($i < $timeMax + .01) {
    > print "DOOD - timeMax undef!\n" if (not defined $timeMax);
    > print "$i:$timeMax";
    > unshift @xs, $i;
    > unshift @ys, scalar (grep { $_ > $i && $_ < $i + 0.01 } @times);
    > 390: $i += .01;
    > }
    >
    > This is for a histogram graph, so I'm finding the number of entries in the
    > data set that fall in the current range, starting at zero and going up
    > until the maximum. I know it's not perfect, but for now I'd settle for any
    > results period, because at the moment the output is:
    >
    > 0.35:0.380084
    > 0.36:0.380084
    > 0.37:0.380084
    > 0.38:0.380084
    > 0.39:0.380084


    Given the value printed above, the program will exit the while loop at
    the next test.

    > Use of uninitialized value in addition (+) at ./stress.pl line 385,
    > <$opt{...}> line 20.
    > DOOD - timeMax undef!


    Here the loop is re-run.

    > Use of uninitialized value in concatenation (.) or string at ./stress.pl
    > line 387, <$opt{...}> line 20.
    > 0:Use of uninitialized value in addition (+) at ./stress.pl line 390,
    > <$opt{...}> line 20.
    >
    > So it seems that $timeMax is suddenly undef'd while inside the loop.


    I don't think so. Your program wandered somewhere else. You should
    print $timeMax before entering the loop.

    HTH

    --
    Dominique Dumont
    "Delivering successful solutions requires giving people what they
    need, not what they want." Kurt Bittner
    Dominique Dumont, Oct 18, 2007
    #9
  10. Dominique Dumont wrote:

    > I don't think so. Your program wandered somewhere else. You should
    > print $timeMax before entering the loop.


    Hm, nope it's correct before the loop. And no other code is executed, it's
    definitely still inside the loop when it goes awry.
    Taylor Venable, Oct 18, 2007
    #10
  11. Brian McCauley wrote:

    > What reason do you have to suspect it was ever defined?


    Because (1) the conditional on it being not defined fails except for the
    last time through the loop, and (2) a value gets printed,
    namely "0.380084". I'm afraid I don't understand you. Is there something
    else amiss here?
    Taylor Venable, Oct 18, 2007
    #11
  12. Brian McCauley wrote:

    > What do references have to do with the price of fish?


    Yeah, you're right. What I was originally considering I think is not
    possible.

    > $timeMax is undef. This will happen if @timing is empty (or indeed if
    > one element of @timing is undef and all the rest are negative, or...).


    None of these are the case.
    Taylor Venable, Oct 18, 2007
    #12
  13. Mumia W. wrote:

    > What do you mean by parallelized?


    Using multiple interpreter threads. But while this code is executed, there
    is only one thread.
    Taylor Venable, Oct 18, 2007
    #13
  14. it_says_BALLS_on_your forehead wrote:

    > what happens if you print Dumper( \@timing ) before and after each
    > time your max sub is called?


    When you dump out @timing you get exactly what I expect, the timing values
    (formatted as floating point numbers) with carriage returns at the end.
    Getting rid of the carriage returns did not change the outcome.
    Taylor Venable, Oct 18, 2007
    #14
  15. Taylor Venable wrote:

    > So it seems that $timeMax is suddenly undef'd while inside the loop. How
    > can this be? The numbers I'm pulling from the file are at the end of the
    > line and haven't been chomp()'d, but I wouldn't think that should matter.


    Well, whatever was going on is gone now. I switched to using a for-loop
    instead, corrected the logical error that the index variable $i was
    incrementing .01 too high, and it's gone. Not sure why that would cause
    this problem, and I can no longer reproduce. Extremely odd; oh well. I'm
    going to switch to using R on the raw data anyway, we need more statistical
    analysis than I care to write in Perl. Thanks for all the suggestions.
    Taylor Venable, Oct 18, 2007
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    941
    Juha Nieminen
    Aug 22, 2007
  2. AAaron123
    Replies:
    9
    Views:
    621
    AAaron123
    Mar 18, 2009
  3. Stef Mientki

    and becomes or and or becomes and

    Stef Mientki, May 22, 2011, in forum: Python
    Replies:
    9
    Views:
    301
    Chris Angelico
    May 28, 2011
  4. Replies:
    15
    Views:
    171
    Brian McCauley
    Sep 23, 2007
  5. Tim Streater
    Replies:
    3
    Views:
    635
    Evertjan.
    Dec 19, 2011
Loading...

Share This Page