in reply to Script for MAX and Min

UPDATE: I changed the code below in response to Merlyn's critique.

Perl is built to manipulate lists.
I'd tell you to put all your scalars into a list and use a loop like this to find your min and max. Why can only scalars be used? That seems horribly ineffecient for the task at hand.
for $nums (@numset){ $min = $nums unless ($min); $max = $nums unless ($max); $max=$nums if ($nums > $max); $min=$nums if ($nums < $min); $total += $nums; ++$count; } print"Minimum: $min","\nMaximum: $max\n","Average: ",$total/$count;
if you insist on scalars or literals put them in the for loop.
for $nums ($first,$second,$third,$forth) or for $nums (123,32,7,-12,-23,17)

also please at least use the <br> and <code> tags, so your program is readable

Replies are listed 'Best First'.
Re: Re: Script for MAX and Min
by merlyn (Sage) on Oct 04, 2001 at 02:24 UTC
    $min = $nums if ($min == undef); $max = $nums if ($max == undef);
    Please note that this code is extremely misleading. undef as used here in a numeric context is exactly replaced with 0, with perhaps a warning if you turn on such.

    The only operator that sees undef as what it is, rather than the empty string or 0, is the defined operator.

    -- Randal L. Schwartz, Perl hacker