#!/var/local/couch/bin/perl 
&header;
open(FILE, "POLL.TXT");
@file_info = <FILE>;
close(FILE);
&get_data(@file_info);
&print_results(@file_info);
&footer;
exit(0);

sub print_results
{ #tabulate the survey results 
  my(@file_info) = @_;
  my($i, $j, $k,  $species, $male,$female);
  my(@species_name) = ("Beaver", "Ferret", "Muskrat", "Mouse", "Opposum");
  my(@species_count) = (0,0,0,0,0);
  my(@qcount) = ([0,0,0], [0,0,0],[0,0,0]);
  my(@qresponse) = ("Yes", "No", "DK");
  my(@qn) = ("Q1", "Q2", "Q3");
  my(@qlabel) = ("1. Merge Muskrat Mounds and Beaver Town School Districts?", "2. Use bond revenue to build a hockey arena?", "3. Retain Judge Finneas Ferret?");
  foreach $i(0..$#file_info)
  { #print the summary statistics for each hash key
       $female++  if ($file_info[$i]{'Sex'} eq "Female");
        $male++    if ($file_info[$i]{'Sex'} eq "Male");
        foreach $j(0..$#species_name)
        { #count the species
               $species_count[$j]++ if($file_info[$i]{'Species'} eq $species_name[$j]);    
          }      
        foreach $j(0..$#qn) 
        {#count each opinion, put in matrix
            foreach $k(0..$#qresponse) {
                   $qcount[$j][$k]++  if ($file_info[$i]{$qn[$j]} eq $qresponse[$k]); 
             }
        }
  }  
  print "<CENTER><B>The Survey Results</B></CENTER><P>\n";
  print "<TABLE BORDER =  '1' CELLPADDING = '3' ALIGN = 'LEFT'>\n";
  print "<CAPTION> <B>Sex</B></CAPTION>\n";
  print "<TR><TH>Female</TH><TH>Male</TH></TR>\n";
  print "<TR><TD> $female </TD><TD>  $male</TD></TR></TABLE><P>\n";  
  print "<TABLE BORDER = '1' CELLPADDING = '3'>\n";
  print "<CAPTION> <B>Species</B></CAPTION><TR>\n";
  foreach $i(0..$#species_name){
  print "<TH>$species_name[$i]</TH>\n";} 
  print "</TR><TR>\n";
   foreach $i(0..$#species_name){
  print "<TH>$species_count[$i]</TH>\n";}
   print "</TR></TABLE><P>\n";
   print "<TABLE BORDER = '1' CELLPADDING = '3'><CAPTION> Survey Questions</CAPTION>\n";
  print "<TR><TH>Question</TH><TH>Yes</TH><TH>No</TH><TH>Don't Know</TH></TR>\n";
  foreach $i(0..@{qcount})
  { #print the matrix of opinion questions and responses
    print "<TR><TD>$qlabel[$i]</TD>\n";
    foreach $j(0..@{$qcount[$i]})
     {print "<TD>$qcount[$i][$j]</TD>\n";}
    print "</TR>\n";
   }
   print "</TABLE>\n";
}  
  
sub get_data
{ #Use Steps 1 -6 to get and decode the data
  #Create an array of hashes for each respondent
  my($size, $form_size, $key, $value, $i, $j, $element);
  my(@fields);
  my(@file_info) = @_;
  $i = 0;
  foreach $element(@file_info)
  {#parse the data --> create an array of hashes
    $element =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
    @fields = (split(/&/, $element));
    $hash_size = @fields;
    for ($j=0; $j < $hash_size; $j++){
       ($key, $value) = (split /=/, $fields[$j]);
       $value =~ s/[;<>\(\)\{\}\*\|'`\&\$!#:"\\]/\ $1/g;
       $value =~ s/[+]/\ $1/g;
       chomp($value);
       $file_info[$i]{$key} = $value;
       
     }
     $i++;
   }
   @_ = @file_info;
}   
    
 
   
sub header
{# print the header for the HTML page
   print "Content-type: text/html", "\n\n";
   print "<HTML><HEAD><TITLE></TITLE></HEAD>\n";
   print "<BODY BGCOLOR = 'white'>\n";
}

sub footer
{# print the footer for the html page
   print "</BODY></HTML>";
}

