Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl help, very confused.

Status
Not open for further replies.

eitremn

Programmer
Sep 30, 2009
3
US
i have a cgi file to display the league bowlers in a data file. it works just fine as long as their number of games, and scores aren't 0 (which it is set to by default). why wouldn't this print out the bowler information if those values are set to 0?

Code:
#!/usr/local/bin/perl
print "Content-Type: Text/html\n\n";

open (bowlers, "bowl.dat") or die ("file didn't open");
@bowlerInfo = <bowlers>;
close (bowlers);
chomp(@bowlerInfo);

print "<html>\n";
print "<head>\n";
print "<title>Lake Side Bowling League:  View all.</title>\n";
print "</head>\n";
print "<body>\n";
print "<h1>Lake Side Bowling League</h1>\n";
print "<h3>Individual Scores</h3>\n";
print "<table border='1'>\n";
print "<tr><td><b><center>Name</center></b></td><td><b><center>Average</center></b></td>
	   <td><b><center>High</center></b></td><td><b><center>Games</center></b></td></tr>\n";
foreach(@bowlerInfo)
{
	($id, $lName, $fName, $total, $numGames, $hScore) = split(/#/, $_);
	$average = $total / $numGames;
	$average = sprintf("%.2f", $average);
	
	print "<tr><td>$fName $lName</td><td>$average</td><td>$hScore</td><td>$numGames</td></tr>\n";
}
print "</table><br />\n";
print "<a href='[URL unfurl="true"]http://tacosalad.lssu.edu/~neitrem/csci333/bowling/index.html'>Back[/URL] to main</a>\n";
print "</body>\n";
print "</html>\n";
 
You should:

use strict;
use warnings;
use CGI; # and others if possible.

open (bowlers, "bowl.dat") or die ("file didn't open");
# Does bowl.dat have any records in it?
# Have you tried adding the absolute path to bowl.dat?
# Try printing out the contents of bowl.dat

Also, split \#
$average = ($total / $numGames);


 
[tt]$numGames[/tt] can't be zero as you are dividing by it to get the average. You should get an error page when there is at least one zero [tt]$numGames[/tt].
What are you actually receiving?
Also post an example line with zero [tt]$numGames[/tt] or [tt]$hScore[/tt].

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
ok, i fixed the dividing by zero issue w/ an if statement and everything is working ok. thanks for the help! in case you were still curious, the file format looked like:

id#lastname#firstname#highscore#numgames#totalpins

so when the user creates a new bowler i was having it output to the file as:

id#lastname#firstname#0#0#0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top