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

Hi, I'm very new to Perl, (I've just started learning about Arrays) but I can't for the life of me figure out how to code a Median into my fairly basic program (find the smallest number and the largest numbers that the user has entered and then calculate the Median. The program won't quit until the user enters a negative so it could be an infinite amount of numbers the user enters so that's where I'm a bit confused into trying to code a Median. Thank you all for taking time out of your day to help me in advance just a general gist in the right direction would be a ton of help

  • Comment on Perl Newbie trying to figure out how to find out Median in my program

Replies are listed 'Best First'.
Re: Perl Newbie trying to figure out how to find out Median in my program
by NetWallah (Canon) on Apr 19, 2017 at 21:17 UTC
    Here is a "one liner" for the median.

    Use Single quotes if run on Linux.

    perl -ne "chomp;last if $_ <0; push @val, $_}{@s=sort {$a<=>$b} @val;p +rint qq||,($s[$#s/2+0.1]+ $s[$#s/2+0.6])/2,qq|\n|"
    See how to find median value for the calculation details.

    If curious, lookup the Eskimo Greeting operator that is used here.

            ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

      If curious, lookup the Eskimo Greeting operator that is used here.
      That's wonderfully evil, and you should be ashamed of yourself for foisting it on a newbie.
              >> you should be ashamed of yourself

        Well - I am sporting a slight smirk ...

        In the absence of code from the OP, I figured I'd offer more of a learning opportunity ...

        That's my excuse, and I'm sticking to it.

                ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

Re: Perl Newbie trying to figure out how to find out Median in my program
by Corion (Patriarch) on Apr 19, 2017 at 20:09 UTC

    Consider that the Median of a set is the number where half of all elements of the set are smaller than that number and half of them are larger than that number.

    If you have a list that is sorted, the median will be somewhere close to the middle of the list.

    If the list has an odd number of elements, it will be right in the middle of the list.

    If you show us your code, we can likely give better advice that is more to the point.

      My apologies. This is what I have so far.

      #!/usr/bin/perl use Modern::Perl; my ($total, $howmany, $largest, $smallest,$number, $average) = (0, + 0 +, 0, 0, 0, 0); print "Enter a value, negative value to end "; chomp ($number =<> ); $smallest = $number; $largest =$number; while ($number != -1) { $total += $number; $howmany += 1; $smallest = $number if $number < $smallest; $largest = $number if $number > $largest; $median = ($number, $number, $number print "Enter a value, negative value to end "; chomp ($number =<>); } if ($howmany == 0 ) { say "There were no values to process. "; } else { say "AVG: ", $total / $howmany; say "LRG: ", $largest; say "SML: ", $smallest; say "MED: ", }
        You're going to need to store all the numbers in an array, then find the median once you have them all.
Re: Perl Newbie trying to figure out how to find out Median in my program
by Anonymous Monk on Apr 19, 2017 at 20:06 UTC
    wot u r code look alike?