User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,759 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 1493 | Replies: 10 | Solved
Reply
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Question File utilization

  #1  
Dec 2nd, 2007
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?)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 302
Reputation: eggi is on a distinguished road 
Rep Power: 2
Solved Threads: 29
eggi eggi is offline Offline
Posting Whiz

Re: File utilization

  #2  
Dec 2nd, 2007
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;


Originally Posted by kahaj View Post
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?)
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: File utilization

  #3  
Dec 3rd, 2007
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?
Reply With Quote  
Join Date: Mar 2006
Posts: 641
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: File utilization

  #4  
Dec 3rd, 2007
You need to clarify your question and post any code you have written so far.
Reply With Quote  
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: File utilization

  #5  
Dec 3rd, 2007
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;
Reply With Quote  
Join Date: Mar 2006
Posts: 641
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: File utilization

  #6  
Dec 3rd, 2007
Not only is the code not complete, it has a number of errors. Fix the errors before continuing.
Reply With Quote  
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: File utilization

  #7  
Dec 4th, 2007
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
Reply With Quote  
Join Date: Mar 2006
Posts: 641
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: File utilization

  #8  
Dec 4th, 2007
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>
};
Reply With Quote  
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: File utilization

  #9  
Dec 4th, 2007
Okay, I've changed that. But I really need to figure out how to do the things I initially asked about...
Reply With Quote  
Join Date: Mar 2006
Posts: 641
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: File utilization

  #10  
Dec 5th, 2007
Originally Posted by kahaj View Post
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;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 9:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC