I presume(perhaps wrongly) that you mean something like the following:

#!/usr/bin/perl -w use strict; use warnings; sub mean { my (@data) = @_; my $sum; foreach (@data) { $sum += $_; } return ( $sum / @data ); } sub median { my (@data) = sort { $a <=> $b } @_; if ( scalar(@data) % 2 ) { return ( $data[ @data / 2 ] ); } else { my ( $upper, $lower ); $lower = $data[ @data / 2 ]; $upper = $data[ @data / 2 - 1 ]; return ( mean( $lower, $upper ) ); } } sub std_dev { my (@data) = @_; my ( $sq_dev_sum, $avg ) = ( 0, 0 ); $avg = mean(@data); foreach my $elem (@data) { $sq_dev_sum += ( $avg - $elem )**2; } return ( sqrt( $sq_dev_sum / ( @data - 1 ) ) ); } my ( $data, @dataset,%h,$lc ); my $file = "fake.txt"; open(IN, "<", $file) or die "can't open $file $!\n"; while (<IN>) { chomp; $lc++; $h{$lc} = $_; } close IN; #while (<DATA>) { # chomp; # $lc++; # $h{$lc} = $_; #} #@dataset = (values %h); print "vals " .scalar(keys %h) . "\n"; print "Median: ", median(@dataset), "\n"; print "Mean: ", mean(@dataset), "\n"; print "Standard Dev.: ", std_dev(@dataset), "\n"; __DATA__ 1 4 2 7 89 5 2 8 6 23

If this assignment actually requires you to process the data in the subs by handing in the hash instead of the array, you should find enough here to get you started... . Run it with 'perl -d' to see what things look like. The debugger is easy to use.

One also has to presume that 'fake.txt' looks like __DATA__, which you did not specify.

...the majority is always wrong, and always the last to know about it...

Insanity: Doing the same thing over and over again and expecting different results...

A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is a facct


In reply to Re: Mean, Median and Mode by wjw
in thread Mean, Median and Mode by Noob@Perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.