news.utdallas.edu!convex!tchrist Tue Dec 15 10:08:26 CST 1992
Article: 64 of comp.lang.perl
Xref: feenix.metronet.com comp.lang.perl:64
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.utdallas.edu!convex!tchrist
From: Tom Christiansen <[email protected]>
#Subject: Re: Statistic routines ?
Originator: [email protected]
Sender: [email protected] (news access account)
Message-ID: <[email protected]>
Date: Tue, 15 Dec 1992 15:50:17 GMT
Reply-To: [email protected] (Tom Christiansen)
References: <[email protected]>
Nntp-Posting-Host: pixel.convex.com
Organization: Convex Computer Corporation, Colorado Springs, CO
Keywords: statistic
X-Disclaimer: This message was written by a user at CONVEX Computer
             Corp. The opinions expressed are those of the user and
             not necessarily those of CONVEX.
Lines: 65

>From the keyboard of [email protected] (Otmar Lendl):
:I have to do some statistical analysis of data I gather with
:perl scripts, so it would be convenient to use perl for that
:purpose, too.
:
:Before I start to write my own set of functions (Mean, standard-deviation,
:histograms, ...) I better ask if there are already such beasts.
:I checked the script-archives listed in the FAQs (convex, ohio-state),
:but could not find anything suitable.

Here's something I hacked out yesterday.  The format is

title<tab>a b c d e

where the letters are the number of scores of value 1..5;

--tom


#/usr/bin/perl
$[ = 1;
while (<>) {
   if (/^\s*$/) {
       print;
       next;
   }
   $v = $mean = $sum = $n = $sdev = 0;

   ($title, $figures) = /^([^\t]+)\t+(.*)/;
   split(' ',$figures);

   for ($i = 1; $i <= 5; $i++) {
       $n += $_[$i];
       $sum += $i * $_[$i];
   }

   $mean = $sum / $n;

   for ($i = 1; $i <= 5; $i++) {
       for ($j = 0; $j < $_[$i]; $j++) {
           $v += ($mean - $i) ** 2;
       }
   }
   $v /= $n;
   $sdev = sqrt($v);

   write;

}

format STDOUT_TOP =
Course Title                   N     Mean   Std Dev
==========================    ===    =====  =======