in reply to undef number?

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.

Replies are listed 'Best First'.
Re^2: undef number?
by ikegami (Patriarch) on Apr 19, 2010 at 16:09 UTC

    the easy way is to just sort @x and take the last element with a slice...

    There's no way I would call

    (sort {$a <=> $b} @x)[-1,0]
    easier than
    max(@x)

    They're both core, max being located in List::Util.

      Didn't realize this was core, but indeed it is! And also List::Util::XS so one can see if List::Util was installed as C compiled version. Sometimes it is easy to forget that the Camel book only has a subset of Core modules listed as there are a lot of them now.

      thanks.