Perl Basics and Biostatistics

Tallying Results

In this lesson the student will learn how to:
  1. Implement a never ending loop
  2. End a loop at any point within it's execution
  3. Use the tab symbol
  4. Use the chop function
  5. Increment a scalar
  6. Add values to a list
By the end of this lesson the student will be able to:

  Write a script which tallies measurments and calculates the 
  mean using this tally.

Let's say that the state decides for some reason that it needs to know the height of all the nine-year-old girls in the town of Trona. So, some teacher or administrator or public health official is given the task of measuring all the nine-year-old girls attending Trona Elementary School. In the process of recording these measurements this person might create a tally that looks something like this:

Height of Nine-Year-Old Girls in Trona, 2003:

 height    number
   3'11"     1
   4' 0"     2
   4' 1"     1
   4' 2"     0
   4' 3"     2
   4' 4"     2
   4' 5"     1
   4' 6"     0
   4' 7"     1
   4' 8"     0
   4' 9"     2
   4'10"     1
   4'11"     0
   5' 0"     0
   5' 1"     1

Using this data we can easily calculate an average height after converting it into inches.

47+48+48+49+51+51+52+52+53+55+57+57+58+61 = 739
  739 / 14 = 52.79

Here's a little script which may make tallying our information a little easier:
#!/usr/bin/perl @keys = (); @values = (); print "Enter Tally Title: "; $title = <STDIN>; chop($title); #populate our lists while(true){ #endless while loop print "Enter height in inches: "; $ht = <STDIN>; chomp($ht); if($ht eq 'q'){ last; } print "Enter number of subjects for that height: "; $n = <STDIN>; chomp($n); push(@keys, $ht); push(@values, $n); } #print results $cnt = 0; print "\n\n$title\n\n"; print "HEIGHT\tNUMBER\n"; while($cnt < scalar(@keys) ){ print "@keys[$cnt]\t@values[$cnt]\n"; $cnt++; }
Sample output:

Height of Dwarfs in Fairyland

HEIGHT  NUMBER
14      2
16      3
17      1
19      2
21      1

Inspect the script shown above (after executing it a few times) and consider the following points:

ASSIGNMENT:

Extend the sample script so that it calculates the mean for your data points. Report the mean after displaying your tally.