Perl Basics and Biostatistics
Tallying Results
In this lesson the student will learn how to:
- Implement a never ending loop
- End a loop at any point within it's execution
- Use the tab symbol
- Use the chop function
- Increment a scalar
- 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 = ;
chop($title);
#populate our lists
while(true){ #endless while loop
print "Enter height in inches: ";
$ht = ;
chomp($ht);
if($ht eq 'q'){
last;
}
print "Enter number of subjects for that height: ";
$n = ;
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:
- Infinite Loop - The while(true) { #do stuff } loop in this script
will never end. It will continue on forever (or until the power goes out).
The only thing that will stop it (besides pulling the plug out of the back
of your computer or an interrupt signal from the operating system) is if the
user enters a single 'q' when prompted to enter a height. This will trigger
a call to the last function. The last function will end this otherwise
infinite loop.
- Using TABS - Sometimes you want to create rows when printing your
output. The \t symbol is useful for this. You can use two \t symbols in a
row to create a double tab if you want.
- The chop Function - The chop function just chops off the last
character in a string. In this script it will chop off the \n character in
the $title varible.
- Auto-incrementation - The $cnt++; statement is a shorthand way of
adding one to the $cnt variable. The $cnt is used as a counter in the last
while loop. This loop could be made into an infinite loop if we didn't have
the $cnt++; statement inside it because without the $cnt++; statement the
value of $cnt will never become greater than the length of @keys which is
the condition which ends the loop!
- The scalar Function - The scalar function returns the length of
a list.
- Adding Values to a List - First of all we create an empty list like this: "@vals = ( );". We
populate this list using the push function. The push function takes two
arguments: The first is the list into which we add the second argument.
ASSIGNMENT:
Extend the sample script so that it calculates the mean for your data
points. Report the mean after displaying your tally.