Eily has asked for the wisdom of the Perl Monks concerning the following question:
Hello fellow monks. This is actually the first time I end up posting here, because until now, I always found my answer while writing my question, or just before clicking "create". You were all my rubber ducks :). But I can't understand this one.
On the version of perl I have here (strawberry v5.20), this code yields two warnings:
use strict; use warnings; use List::Util qw( min ); use Data::Dump qw( pp ); my (@_x, @_y); my @one = 1; my @two = 0.5; pp { one => $#one, two => $#two }; pp @one, @two; for my $_in (1..5) { push @_x, "$_in"; my $_out = 0; #pp "$#_x", "$#one", min $#_x, $#one; #pp "$#_y", "$#two", min $#_y, $#two; my $x_max = min $#_x, $#one; my $y_max = min $#_y, $#two; pp { x => $x_max, y => $y_max }; $_out += $one[$_] for 0..$x_max; $_out -= $two[$_] for 0..$y_max; push @_y, $_out; } pp { one => $#one, two => $#two }; pp @_y;
Use of uninitialized value in addition (+) at test.pl line 28Unless I'm mistaken, this means I'm reaching for elements outside of @one and @two, even though $x_max and $y_max should not exceed the highest index.
Use of uninitialized value in subtraction (-) at test.pl line 29
I don't understand how with $#one == 0 and $x_max = min $#_x, $#one I find that $x_max > 0. What surprises me even more is that the warning disappears when I add the two commented lines (removing the quotes shows that min returns an alias and not a copy of the value).
Did I miss something obvious, or is this some weird side effect of my code?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: min $#a, $#b > $#b
by toolic (Bishop) on Jan 12, 2016 at 19:32 UTC | |
by Eily (Monsignor) on Jan 12, 2016 at 19:55 UTC | |
Re: min $#a, $#b > $#b
by Anonymous Monk on Jan 12, 2016 at 21:16 UTC | |
by Eily (Monsignor) on Jan 13, 2016 at 08:50 UTC | |
by ikegami (Patriarch) on Jan 15, 2016 at 20:29 UTC | |
Re: min $#a, $#b > $#b
by FreeBeerReekingMonk (Deacon) on Jan 12, 2016 at 19:42 UTC | |
by Crackers2 (Parson) on Jan 12, 2016 at 20:47 UTC | |
by Eily (Monsignor) on Jan 13, 2016 at 08:55 UTC | |
by FreeBeerReekingMonk (Deacon) on Jan 12, 2016 at 21:11 UTC | |
by Mr. Muskrat (Canon) on Jan 12, 2016 at 21:22 UTC | |
by FreeBeerReekingMonk (Deacon) on Jan 12, 2016 at 21:38 UTC | |
by Eily (Monsignor) on Jan 12, 2016 at 19:57 UTC |