NodeReaper has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Script for MAX and Min
by pjf (Curate) on Oct 04, 2001 at 09:26 UTC
    Forget all the suggestions you've been given above, real programming students use Quantum::Superpositions to find the maximum and minimum numbers in a list and impress their teachers.
    use Quantum::Superpositions; $max = any(@items) >= all(@items); $min = any(@items) <= all(@items);
    This runs in constant time on non-deterministic machines, making it the most efficient solution to the problem by far.

    Cheers,
    Paul

Ask your teacher
by petdance (Parson) on Oct 03, 2001 at 23:18 UTC
    I'd suggest that you ask your programming teacher to help you. You'll undoubtedly get more benefit out of the assignment if you work with the instructor directly, rather than outsiders. If we do it for you, you've learned nothing, and that's not what the Monastery is about.

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

      yeah, if only there were a construct in perl that could *test* for conditions, and if only there were some way of giving some piece of information a name, putting that information in a holder of some sort, and to refer to that information by that name, and maybe even manipulating the information. In fact, wouldn't it be great if you could tell perl

      For every value you have 1. IF the current value is greater than the biggest one you've seen so + far, THEN it's the new biggest one you've seen. 2. IF the current value is lower than the lowest value you've seen, THEN it's the new lowest one you've seen
      ?

      And if only it were the case that at the end of such a process, "the biggest value you've seen so far" would have to be the biggest value in the collection, and similarly for the lowest value you'd seen. But them's the breaks, as they ... well, nobody I know ... say.

      perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Script for MAX and Min
by dragonchild (Archbishop) on Oct 04, 2001 at 00:40 UTC
    Here ya go. Hope you can tell your teacher what it does...
    #!/usr/bin/perl *=*j=>*-=*k=>*>=*l=>*<=*m; while(<>){chomp;$*++;$-+=$_;($_>$>)&&($>=$_);($_<($<||=$>))&&($<=$_)}$ +-/=$*; print"$> $- $<\n"

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Script for MAX and Min
by runrig (Abbot) on Oct 04, 2001 at 00:21 UTC
    Hope you can explain this one to your instructor. It may not be the most efficient way to do it (oh yeah, and it pads your numbers with zeros, but that's ok, isn't it?) :)
    Package MinMax; sub new { my $proto = shift; my $class = ref($proto) || $proto; bless [ sort map { sprintf "%09d", $_ } @_ ], $class; } sub min { @{shift()}[0]; } sub max { @{shift()}[-1]; } sub total { my $self = shift; my $total = 0; $total += $_ for @$self; $total; } sub avg { my $self = shift; $self->total/@$self; }
    I've given you the methods, you just need to call them. You can check out perlboot :)
Re: Script for MAX and Min
by tstock (Curate) on Oct 04, 2001 at 00:49 UTC
    Tell your teacher that loops are not the best solution for a min/max/average solution, because they are s-l-o-w.

    You don't need to step through all the values to get the min and max , you can just sort the list and get the first and last value, since sorting is *much* simpler for the perl interpreter than having to look at each value. chezzz.

    ($min, $max) = (sort { $a <=> $b } @numset)[0,-1];

    As for the average, what could be faster than just adding all the numbers divided by the number of numbers ? Several divisions on smaller numbers are easier than dividing the big number in the end, so this is FAST!

    grep { $avg += $_/@numset } @numset;

    Don't worry about it being hard to read, perl is all about being hard to read and you are likely to be looked upon with admiration from your colleges for code like this!

    I sit back and watch the XP's fall...

Re: Script for MAX and Min
by pixel (Scribe) on Oct 04, 2001 at 00:24 UTC

    Why bother doing all that work when someone else has already done it for you? I think you'll find the List::Util module in the Scalar-List-Utils bundle will do all you want.

    use List::Util; # assume data in @data my $max = max @data; my $min = min @data; my $sum = reduce { $a + $b } @data; my $avg = $sum / @data;

    Blessed Be
    The Pixel

Re: Script for MAX and Min
by thunders (Priest) on Oct 03, 2001 at 23:55 UTC
    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
      $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