variable bewilderingly becomes undefined

T

Taylor Venable

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
 
I

it_says_BALLS_on_your forehead

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.
 
T

Taylor Venable

it_says_BALLS_on_your forehead said:
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.
 
I

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
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?
 
M

Mumia W.

[...]
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?
 
B

Brian McCauley

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.
 
B

Brian McCauley

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...).
 
D

Dominique Dumont

Taylor Venable said:
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
 
T

Taylor Venable

Dominique said:
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.
 
T

Taylor Venable

Brian said:
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?
 
T

Taylor Venable

Brian said:
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.
 
T

Taylor Venable

it_says_BALLS_on_your forehead said:
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.
 
T

Taylor Venable

Taylor said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,851
Latest member
CliftonCor

Latest Threads

Top