home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7436 < prev    next >
Encoding:
Text File  |  1992-12-15  |  2.3 KB  |  84 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!spool.mu.edu!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Statistic routines ?
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Dec15.155017.14450@news.eng.convex.com>
  8. Date: Tue, 15 Dec 1992 15:50:17 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <Bz7qCA.Gy5@cosy.sbg.ac.at>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. Keywords: statistic
  14. X-Disclaimer: This message was written by a user at CONVEX Computer
  15.               Corp. The opinions expressed are those of the user and
  16.               not necessarily those of CONVEX.
  17. Lines: 65
  18.  
  19. From the keyboard of lendl@cosy.sbg.ac.at (Otmar Lendl):
  20. :I have to do some statistical analysis of data I gather with
  21. :perl scripts, so it would be convenient to use perl for that
  22. :purpose, too.
  23. :
  24. :Before I start to write my own set of functions (Mean, standard-deviation,
  25. :histograms, ...) I better ask if there are already such beasts.
  26. :I checked the script-archives listed in the FAQs (convex, ohio-state),
  27. :but could not find anything suitable.
  28.  
  29. Here's something I hacked out yesterday.  The format is 
  30.  
  31. title<tab>a b c d e
  32.  
  33. where the letters are the number of scores of value 1..5;
  34.  
  35. --tom
  36.  
  37.  
  38. #!/usr/bin/perl
  39. $[ = 1;
  40. while (<>) {
  41.     if (/^\s*$/) {
  42.     print;
  43.     next;
  44.     } 
  45.     $v = $mean = $sum = $n = $sdev = 0;
  46.  
  47.     ($title, $figures) = /^([^\t]+)\t+(.*)/;
  48.     split(' ',$figures);
  49.  
  50.     for ($i = 1; $i <= 5; $i++) {
  51.     $n += $_[$i];
  52.     $sum += $i * $_[$i];
  53.     } 
  54.  
  55.     $mean = $sum / $n;
  56.  
  57.     for ($i = 1; $i <= 5; $i++) {
  58.     for ($j = 0; $j < $_[$i]; $j++) {
  59.         $v += ($mean - $i) ** 2;
  60.     } 
  61.     }
  62.     $v /= $n;
  63.     $sdev = sqrt($v);
  64.  
  65.     write;
  66.     
  67.  
  68. format STDOUT_TOP = 
  69. Course Title                   N     Mean   Std Dev
  70. ==========================    ===    =====  =======
  71. .
  72.  
  73. format STDOUT = 
  74. @<<<<<<<<<<<<<<<<<<<<<<<<<    @##    @#.##  @#.##
  75. $title,                 $n,     $mean, $sdev
  76. .
  77.  
  78. -- 
  79.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  80.     Even if you aren't in doubt, consider the mental welfare of the person who
  81.     has to maintain the code after you, and who will probably put parens in
  82.     the wrong place.  --Larry Wall in the perl man page 
  83.