
perlmachine
Deleted
Jun 11, 2000, 7:03 PM
Post #1 of 2
(205 views)
|
after opening a text file for reading and passing the contents into an array i'm running into problems. for whatever reason functions like length(@array), $#array, and so on are only working with the first character of the array. i know the array contains more than one character by printing it out. source is below if that helps. #!/usr/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser); # create CGI object instance $cgiobject = new CGI; # get values submitted by user $File = $cgiobject->param('Site_Link'); $Rating = $cgiobject->param('Rating'); $Directory = "/home/broadban/www/cgi-bin/ratings"; # open W(rite to)FILE, lock it, and go to last line open(WFILE,">>$Directory/$File") or die "Could not open file"; flock(WFILE,2); seek(WFILE,0,2); print WFILE "$Rating,\r"; close(WFILE); # open file to read open(RFILE,"$Directory/$File")or die "Could not open file"; seek(RFILE,0,0); # then read contents into variable @data = <RFILE>; # find number of characters in array (num. of votes) $votetotal = ($#data + 1); # for each score in the file add it to a total score var foreach $element (@data) { $scoretotal =($scoretotal + $element); } # find avg score by dividing num of votes by total points if ($votetotal eq "0") {$avgscore = "NA";} else { $avgscore = ($scoretotal / $votetotal) ;} # output HTML header to web browser print $cgiobject->header; # print avg score and num of votes print "<HTML><BODY>"; print "<CENTER>Average Score= $avgscore</CENTER>"; print "<BR>"; print "<BR>"; print "<CENTER>Total Votes= $votetotal</CENTER>"; print "<BR>"; print "<BR>"; print "<CENTER>@data</CENTER>"; print "<BR>"; print "<BR>"; print "<CENTER>$scoretotal</CENTER>"; print "</BODY></HTML>"; thanks for reading all the way down to here
|