#!C:/perl/bin/perl.exe use CGI::Carp qw(fatalsToBrowser); use CGI; $query = new CGI; print $query->header; print $query->start_html(-title=>'foreach'); # Open a data file and dump ALL the data into an array called @data # Note: The datafile can be viewed here: http://www.web-research-design.net/P593/form3.txt open(INFO, "$ENV{'DOCUMENT_ROOT'}/P593/form3.txt"); @data = ; close(INFO); # cycle through each element of the array # take the element/key and assign it to an array called @a. # use the split command to separate each piece of information in the array where a comma appears # print the 7th element foreach $key (@data){ @a=split(/,/,$key); print "$a[7]
"; } # Step it up a notch. Create a sample mean $thisSum= 0; $thisN= 0; foreach $key (@data){ @a=split(/,/,$key); if($a[7] > 0){ $thisSum= $thisSum + $a[7]; $thisN= $thisN + 1; } } if($thisN > 10){ $thisMean= sprintf("%.3f",$thisSum/$thisN); } else { $thisMean = "Average cannot be computed--too few cases"; } print "The sample average is: $thisMean
"; print $query->end_html;