Home: Perl Programming Help: Beginner:
array help


Jun 11, 2000, 7:03 PM

Views: 1135
array help

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.

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";

print WFILE "$Rating,\r";


# open file to read
open(RFILE,"$Directory/$File")or die "Could not open file";


# 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


Jun 11, 2000, 9:32 PM

Views: 1135
Re: array help

Instead of

print WFILE "$Rating,\r";

try this

print WFILE "$Rating\n";