I would suggest a different method... In Perl if you have some @x and you want to find the maximum value, the easy way is to just sort @x and take the last element with a slice...like below. The -1 index means "last one". Note that this will work for max,min even if there is only one thing in @x. Now granted sort is slower than some one pass "is this the biggest algorithm" but it is very simple to use and is faster than you might think. Use the power of the language and optimize later if you need to.
#!/usr/bin/perl -w use strict; my @x =(56, 78, 3, -1, 15); my ($max, $min) = (sort {$a <=> $b} @x)[-1,0]; print "max = $max, min =$min\n"; #prints: max = 78, min =-1
updated: numeric sort
the default is alphabetic comparison and that would give -1 15 3 56 7 78 which of course isn't right! So specifying $a <=> $b specifies the "spaceship" operator, a numeric comparison rather than the default of $a cmp $b which is an alpha comparison.

update again: see ikegami's post.


In reply to Re: undef number? by Marshall
in thread undef number? by Anonymous Monk

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.