variable bewilderingly becomes undefined
variable bewilderingly becomes undefined
am 17.10.2007 15:54:00 von 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
Re: variable bewilderingly becomes undefined
am 17.10.2007 16:13:25 von it_says_BALLS_on_your forehead
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.
Re: variable bewilderingly becomes undefined
am 17.10.2007 16:26:45 von Taylor Venable
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.
Re: variable bewilderingly becomes undefined
am 17.10.2007 16:37:05 von it_says_BALLS_on_your forehead
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?
Re: variable bewilderingly becomes undefined
am 17.10.2007 18:01:18 von paduille.4061.mumia.w+nospam
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?
Re: variable bewilderingly becomes undefined
am 17.10.2007 23:12:41 von nobull67
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()?
Re: variable bewilderingly becomes undefined
am 17.10.2007 23:21:13 von nobull67
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.
Re: variable bewilderingly becomes undefined
am 17.10.2007 23:26:30 von nobull67
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...).
Re: variable bewilderingly becomes undefined
am 18.10.2007 14:13:09 von dominique.dumont
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
Re: variable bewilderingly becomes undefined
am 18.10.2007 15:14:45 von Taylor Venable
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.
Re: variable bewilderingly becomes undefined
am 18.10.2007 15:16:19 von Taylor Venable
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?
Re: variable bewilderingly becomes undefined
am 18.10.2007 15:18:33 von Taylor Venable
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.
Re: variable bewilderingly becomes undefined
am 18.10.2007 15:19:18 von Taylor Venable
Mumia W. wrote:
> What do you mean by parallelized?
Using multiple interpreter threads. But while this code is executed, there
is only one thread.
Re: variable bewilderingly becomes undefined
am 18.10.2007 15:20:28 von Taylor Venable
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.
Re: variable bewilderingly becomes undefined
am 18.10.2007 15:27:43 von Taylor Venable
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.