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 28
Use of uninitialized value in subtraction (-) at test.pl line 29
Unless 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.

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?


In reply to min $#a, $#b > $#b by Eily

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.