in reply to Printing largest number in array using loops and if statement
The implementation you have come up with is usually referred to as the high water mark algorithm. Usually, it looks more like this:
my $max; for (@list) { $max = $_ if ! defined $max || $_ > $max; }
If you know for that no value will be less than a certain number, you can set it to that (as you did with 0) or you can assign it to the first value in the list (as suggested elsewhere in this thread) and then loop through the rest of the list. Many would recommend not re-inventing the wheel and would point you to List::Util.
I wrote How A Function Becomes Higher Order which may be too advanced but keep the link around for when you are ready. Here are some extra credit ideas to make you consider how you might need to modify the water mark algorithm:use List::Util 'max'; print max(@list), "\n";
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Printing largest number in array using loops and if statement
by matze77 (Friar) on Jan 17, 2010 at 08:39 UTC | |
by dsheroh (Monsignor) on Jan 17, 2010 at 11:53 UTC |