I'm writing to a file. There are only ten items able to be written to the file (game scores, test results, whatever). I'm using a drop-down selection list on the form to tell which number is being submitted. Is there any way to have it checked as the page loads what numbers have been submitted and omit them from the selection list? (Say numbers 1, 2, 4, and 8 have been submitted; then only 3, 5, 6, 7, 9, & 10 would be available.)

Also, how would I go about averaging something already submitted into the file with new data as it comes in? (An item that cost $2 is already in the file. An item that costs $1 is submitted. I want the average of $1.50 outputted. How would I accomplish this?)

Recommended Answers

All 10 Replies

Hi,
I'm not sure I follow on the first one, but for the second part, this might help:

$already_putin=2.00 # use 0 to start;
$input = 1.00;
$mean_average = ($already_putin+$input)/2;

I'm writing to a file. There are only ten items able to be written to the file (game scores, test results, whatever). I'm using a drop-down selection list on the form to tell which number is being submitted. Is there any way to have it checked as the page loads what numbers have been submitted and omit them from the selection list? (Say numbers 1, 2, 4, and 8 have been submitted; then only 3, 5, 6, 7, 9, & 10 would be available.)

Also, how would I go about averaging something already submitted into the file with new data as it comes in? (An item that cost $2 is already in the file. An item that costs $1 is submitted. I want the average of $1.50 outputted. How would I accomplish this?)

How can I make the numbers average something other than 2? I need it to be able to automatically calculate how much it needs to be divided by. Every time $input is entered, I need to be able to add it to $already_putin and divide it by one more than last time. How do I manage this?

You need to clarify your question and post any code you have written so far.

Keep in mind that the code is in no manner complete.

If it's possible (and I honestly have no clue if it is or not) I'd like to verify before this page loads whether or not Week 1, Week 2, etc. has been selected. If so, either not allow it to be shown in the selection list, or at least give an error message saying that "this week already has data submitted please select another" type of thing.

Also, it needs to be able to store the data submitted and provide totals and averages of some of it.

#!C:/Perl/bin/perl.exe
#Football Stats
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use strict;
use SDBM_File;
use Fcntl;

##########variable declarations and input assignments#############################
my ($weekNum, $whereAt, $homeCounter, $AwayCounter, $yourTmScore, $otherTmScore, $passYrdPGame, $rushYrdPGame, $homeCount, $awayCount 
    $sacks, $sacksAllowed, $turnovForcedPerGame, $turnovAllowedPGame, $tds, $tdsAllowed, $passYrdTotal, $passYrdAvg, $rushYrdTotal, 
    $rushYrdAvg, $sacksTotal, $sacksPerGameAvg, $sacksAllowedTotal, $sacksAllowedAvg, $turnovForcedTotal, $turnovForcedAvg,
    $turnovAllowedTotal, $turnovAllowedAvg, $tdsTotal, $tdsAvg, $tdsAllowedTotal, $tdsAllowedAvg, $wins, $losses, $winPercent, 
    $winPercentHome, $winPercentAway, $winLose,);
$weekNum = param('weekNum');
$whereAt = param('gamePlayed');
$yourTmScore = param('yourTmScore');
$otherTmScore = param('otherTmScore');
$passYrdPGame = param('passYrdPGame');
$rushYrdPGame = param('rushYrdPGame');
$sacks = param('sacks');
$sacksAllowed = param('sacksAllowed');
$turnovForcedPerGame = param('turnovForcedPerGame');
$turnovAllowedPGame = param('turnovAllowedPGame');
$tds = param('tds');
$tdsTotal = 0;
$tdsTotal = $tds + $tdsTotal;
$tdsAvg =
$tdsAllowed = param('tdsAllowed');
$tdsAllowedTotal = $tdsAllowed + $tdsAllowedTotal;
$tdsAllowedAvg = 
$rushYrdTotal = $rushYrdPGame + $rushYrdTotal;
$rushYrdAvg = 
$passYrdTotal = $passYrdPGame + $passYrdTotal;
$passYrdAvg = 


########save to file#######################################
open(OUTFILE, ">>","nflStats.txt")
	or die "Error opening nflStats.txt $!, stopped";
print OUTFILE join(':::',
	$weekNum, $whereAt, $yourTmScore, $otherTmScore, $passYrdPGame, $rushYrdPGame, $sacks, $sacksAllowed, $turnovForcedPerGame, 
	$turnovAllowedPGame, $tds, $tdsAllowed), "\n";
close(OUTFILE);

#calculate stats
open(INFILE, "<", "nflStats.txt");
while(<INFILE>) {
	my ($weekNum, $whereAt, $yourTmScore, $otherTmScore, $passYrdPGame, $rushYrdPGame, $sacks, $sacksAllowed, $turnovForcedPerGame, 
	$turnovAllowedPGame, $tds, $tdsAllowed) = split(':::');


############################### do calculations####################################
if ($yourTmScore > $otherTmScore) {
	$wins = $wins + 1;
}
	else {
		$losses = $losses + 1
	}
}


if ($whereAt = "Home") {
	$homeCount = $homeCount +1;
}
	else if {
		($whereAt = "Away") {
			$awayCount = $awayCount + 1;
	}

close INFILE;

#generate page
print <<printHTML;
<html>
<head><title>Current Cowboys Statistics</title></head>
<body bgcolor="#CCCCCC">
<font size="3">Week $weekNum :<br />Played at: $whereAt<br /></font>
</body></html>
printHTML;

Not only is the code not complete, it has a number of errors. Fix the errors before continuing.

I have all of the errors fixed except one. It keeps telling me that it can't find endHTML string terminator before EOF. I don't know why I'm getting this one.

#!C:/Perl/bin/perl.exe
#Football Stats
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use strict;
use SDBM_File;
use Fcntl;

##########variable declarations and input assignments#############################
my ($weekNum, $whereAt, $homeCounter, $AwayCounter, $yourTmScore, $otherTmScore, $passYrdPGame, $rushYrdPGame, $homeCount, $awayCount, 
    $turnovAllowedPGame, $tds, $tdsAllowed, $passYrdTotal, $passYrdAvg, $rushYrdTotal, 
    $rushYrdAvg, $sacksTotal, $sacksPerGameAvg, $sacksAllowedTotal, $sacksAllowedAvg, $turnovForcedTotal, $turnovForcedAvg,
    $turnovAllowedTotal, $turnovAllowedAvg, $tdsTotal, $tdsAvg, $tdsAllowedTotal, $tdsAllowedAvg, $wins, $losses, $winPercent, 
    $winPercentHome, $winPercentAway, $winLose,);
my $weekNum = param('weekNum');
my $whereAt = param('gamePlayed');
my $yourTmScore = param('yourTmScore');
my $otherTmScore = param('otherTmScore');
my $passYrdPGame = param('passYrdPGame');
my $rushYrdPGame = param('rushYrdPGame');
my $sacks = param('sacks');
my $sacksAllowed = param('sacksAllowed');
my $turnovForcedPerGame = param('turnovForcedPerGame');
my $turnovAllowedPGame = param('turnovAllowedPGame');
my $tds = param('tds');
my $tdsTotal = 0;
my $tdsTotal = $tds + $tdsTotal;
my $tdsAvg = '';
my $tdsAllowed = param('tdsAllowed');
my $tdsAllowedTotal = $tdsAllowed + $tdsAllowedTotal;
my $tdsAllowedAvg = '';
my $rushYrdTotal = $rushYrdPGame + $rushYrdTotal;
my $rushYrdAvg = '';
my $passYrdTotal = $passYrdPGame + $passYrdTotal;
my $passYrdAvg = ''; 


########save to file#######################################
open(OUTFILE, ">>","nflStats.txt")
	or die "Error opening nflStats.txt $!, stopped";
print OUTFILE join(':::',
	$weekNum, $whereAt, $yourTmScore, $otherTmScore, $passYrdPGame, $rushYrdPGame, $sacks, $sacksAllowed, $turnovForcedPerGame, 
	$turnovAllowedPGame, $tds, $tdsAllowed), "\n";
close(OUTFILE);

#calculate stats
open(INFILE, "<", "nflStats.txt");
while(<INFILE>) {
	my ($weekNum, $whereAt, $yourTmScore, $otherTmScore, $passYrdPGame, $rushYrdPGame, $sacks, $sacksAllowed, $turnovForcedPerGame, 
	$turnovAllowedPGame, $tds, $tdsAllowed) = split(':::');


############################### do calculations####################################
if ($yourTmScore > $otherTmScore) {
	$wins = $wins + 1;
}
	else {
		$losses = $losses + 1
	}
}


if ($whereAt = "Home") {
	$homeCount = $homeCount +1;
}
	elsif ($whereAt = "Away") {
			$awayCount = $awayCount + 1;
}
close INFILE;

#generate page
print <<endHTML;
<html>
<head><title>Current Cowboys Statistics</title></head>
<body bgcolor="#CCCCCC">
<font size="3">Week $weekNum :<br />Played at: $whereAt<br /></font>
</body></html>
endHTML

Don't bother with heredocs, they are just too perl 4, use an appropriate quote operator like qq:

#generate page
print qq{
<html>
<head><title>Current Cowboys Statistics</title></head>
<body bgcolor="#CCCCCC">
<font size="3">Week $weekNum :<br />Played at: $whereAt<br /></font>
</body></html>
};

Okay, I've changed that. But I really need to figure out how to do the things I initially asked about...

Okay, I've changed that. But I really need to figure out how to do the things I initially asked about...

OK, a generic example:

my $new_var = param('foo');
open (FH, "file.txt");
while(<FH>) {
    my ($foo,$bar) = split(':::');
    my $average = ($foo+$new_var)/2;
    print $average;
}

Okay, I have everything working now. Thanks for your help everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.