An Example: Son of Stats

Just for kicks, I took the final version of the statistics script we've been working with throughout this week, and “subroutinified” it. I broke the script up into its component parts, and put each of them into a subroutine. The actual body of the script, then, does nothing but call individual subroutines. There's no change in behavior to the script itself; just in how it's organized. Listing 11.1 shows the result:

Listing 11.1. The statssubbed.pl Script
1:  #!/usr/local/bin/perl -w
2:
3:  &initvars();
4:  &getinput();
5:  &printresults();
6:
7:  sub initvars {
8:      $input = "";  # temporary input
9:      @nums = ();   # array of numbers;
10:     %freq = ();   # hash of number frequencies
11:     $maxfreq = 0; # maximum frequency
12:     $count = 0;   # count of numbers
13:     $sum = 0;     # sum of numbers
14:     $avg = 0;     # average
15:     $med = 0;     # median
16:     @keys = ();   # temp keys
17:     $totalspace = 0; # total space across histogram
18: }
19:
20: sub getinput {
21:     while (defined ($input = <>)) {
22:         chomp ($input);
23:         $nums[$count] = $input;
24:         $freq{$input} ++;
25:         if ($maxfreq < $freq{$input} ) { $maxfreq = $freq{$input} }
26:         $count++;
27:         $sum += $input;
28:     }
29:
30: }
31:
32: sub printresults {
33:     @nums = sort { $a <=> $b } @nums;
34:
35:     $avg = $sum / $count;
36:     $med = $nums[$count /2];
37:
38:     print "
Total count of numbers: $count
";
39:     print "Total sum of numbers: $sum
";
40:     print "Minimum number: $nums[0]
";
41:     print "Maximum number: $nums[$#nums]
";
42:     printf("Average (mean): %.2f
", $avg);
43:     print "Median: $med

";
44:     &printhist();
45: }
46:
47: sub printhist {
48:     @keys = sort { $a <=> $b } keys %freq;
49:
50:     for ($i = $maxfreq; $i > 0; $i--) {
51:         foreach $num (@keys) {
52:             $space = (length $num);
53:             if ($freq{$num} >= $i) {
54:                 print( (" " x $space) . "*");
55:             } else {
56:                 print " " x (($space) + 1);
57:             }
58:             if ($i == $maxfreq) { $totalspace += $space + 1; }
59:         }
60:         print "
";
61:     }
62:     print "-" x $totalspace;
63:     print "
 @keys
";
64: }

Because this version of the stats script doesn't do anything functionally different from the one before it, there's only a couple of things to note here:

  • The only part of this script that doesn't live inside a subroutine definition are the lines 3 through 5, which call subroutines to initialize variables, to get the input from a file of numbers, and to calculate the results.

  • You may note that there are four subroutine definitions in this script, but only three subroutine calls at the top of the script. That's because the &printresults() subroutine calls the &printhist() subroutine at the end of its block.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset