in reply to How would I print the last element of a loop?

If this is homework and "spaceship operator" is allowed...to get the larger number:

@nums = (57, 113, 25, 76, 712, 13, 3); foreach (sort {$a <=> $b} @nums) { $larger = $_ } print "The numbers: @nums\n\nlarger=$larger\n";

Replies are listed 'Best First'.
Re: Re: How would I print the last element of a loop?
by Fletch (Bishop) on Apr 02, 2004 at 19:00 UTC

    Erm, you've combined an O(N log N) sort with an O(N) iteration (not to mention always performing N assignemnts); whereas a simple iterative scan will always be just O(N) and will at worst perform N assignments on an already sorted list. Not exactly the most efficient way to find it . . .

    Update: . . . then again maybe we need more answers like this to discourage homework questions. :)