http://qs321.pair.com?node_id=1107854


in reply to I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated.

So as you consider each number now, you're adding it to $total so that it is part of the final average. Similarly you need to keep the largest and smallest numbers you've seen so far. With each new number, after adding it to the $total, see if it qualifies as either the new largest or smallest, and update that value if so.

You can construct your own "if" statements to see if the new number is greater than the current $largest or not. Or you could use List::Util which has a min (and a max) function which return the smallest (and largest) value of the list you pass in. For instance:

use List::Util qw(min max); print max(4,8,2,33,1); # prints 33

So each time you consider a new number you can update the $smallest number so far to either be the current value of $smallest, or the new number:

$smallest = min ($smallest, $number);

Unfortunately because $smallest is set to 0 at the start of your program, no value above zero will ever show up as the smallest input! You could start it off with a value of 10,000 and just hope nobody enters a number bigger than that. But a better way is to start $smallest off with the largest number possible, so that the first input it sees will naturally be the smallest-so-far. In Perl there is a funny notation for this largest number possible: '+inf', and for the smallest possible, minus infinity or '-inf'.

Therefore you'd need to remove $smallest and $largest from where they are initialized to zero now, and add a line like:

my ( $largest, $smallest ) = ('-inf', '+inf');

So that the logic you use to find the largest and smallest is sure to find values that fit.