CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
flat file db help

 



perlmachine
Deleted

Jun 3, 2000, 11:25 AM

Post #1 of 4 (414 views)
flat file db help Can't Post

As my first real perl script (not pieced together parts of other peoples code) this one has probably got quite a few errors. The idea is for one of those "rate the link, or movie" type bits. At this point i can isolate the part where it reads the input and writes to the file and get that to work.
Whether this can be fixed by reworking the syntax or redoing the whole thing i'd appreciate any help.

use CGI;

$cgiobject = new CGI;

# open appropriate ratings file or die
open(RFILE, '/home/broadban/www/cgi-bin/ratings/abcnews.dat') or die "Can't open ratings file.\n";

# read contents into variable
@A = <RFILE>;

# close file
close(RFILE);

# split along lines to find num. of votes for 1,2,3,4,5
@scores = split(/n\/, @A);

$voteone = length($scores[0]);
$votetwo = length($scores[1]);
$votethree = length($scores[2]);
$votefour = length($scores[3]);
$votefive = length($scores[4]);

# find total num of votes
$votetotal = ($voteone + $votetwo + $votethree + $votefour + $votefive);

# find points from each score
$scoreone = (1 * $voteone);
$scoretwo = (2 * $votetwo);
$scorethree = (3 * $votethree);
$scorefour = (4 * $votefour);
$scorefive = (5 * $votefive);

# find total points
$scoretotal = ($scoreone + $scoretwo + $scorethree + $scorefour + $scorefive);

# average score
if ($votetotal == 0) {$avgscore = NA}
else {$avgscore = ($scoretotal / $votetotal)};

# output HTML header to web browser
print $cgiobject->header;

# print message
print "<HTML><BODY>";
print "<CENTER>Average Score= $avgscore</CENTER>";
print "<BR>";
print "<BR>";
print "<CENTER>Total Votes= $votetotal</CENTER>";
print "</BODY></HTML>";


DrZed
User

Jun 3, 2000, 3:20 PM

Post #2 of 4 (414 views)
Re: flat file db help [In reply to] Can't Post

Hmmm... my last response didn't make it because I lost my internet connection while attempting to send it. This time I'll be brief.

$avgscore = NA doesn't look like a valid instruction to me. Perhaps what is meant to be here is $avgscore = 'NA';

Also, you didn't really ask a question or request anything in particual. While you did say you would appreciate help, I have no idea what help you are looking for.

If you have a specific question, just ask.



GoD
Deleted

Jun 4, 2000, 1:19 PM

Post #3 of 4 (414 views)
Re: flat file db help [In reply to] Can't Post

did you put the perl path at the top? an example of a perl path is #!/usr/bin/perl. also did you put this after the perl path? print "Content-Type: text/html\n\n"; this is needed since you are using html with the perl


perlmachine
Deleted

Jun 5, 2000, 2:11 PM

Post #4 of 4 (414 views)
Re: flat file db help [In reply to] Can't Post

last message was probably quite confusing as i was quite confused at the time. now i have split the idea into the three partsi see it as and have found the one thing ireally need help on. (its down toward the bottom) thanks in advance

PART 1 HTML FORM WITH HIDDEN VALUE SPECIFIYING NAME OF CORRESPONDING DATA FILE FOR THIS PAGE AND A SELECT MENU FOR USERS RATING SENT TO APPROPRIATE ADDRESS OF PERL SCRIPT

<HTML>
<BODY>
<FORM METHOD = POST ACTION="http:\\domain.com\cgi-bin\perlscript.pl">
<INPUT TYPE= "HIDDEN"
NAME="Site_Link"
VALUE="specificdata.dat">
<CENTER>Score:<SELECT NAME="Rating">
<OPTION>1
<OPTION>2
<OPTION>3
<OPTION>4
<OPTION>5
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</CENTER>
</FORM>


#!/usr/bin/perl
use CGI;
use CGI::Carp qw(fatalsToBrowser);

# create CGI object instance
$cgiobject = new CGI;

# PART 2 PERL SCRIPT GETS VALUES FROM WEB FORM AND OPENS APPROPRIATE FILE THEN PRINTS APPROPRIATE SCORE FOLLOWED BY DIVIDER ":"

# get values submitted by user
$File = $cgiobject->param('Site_Link');
$Rating = $cgiobject->param('Rating');
$Directory = "/home/user/www/cgi-bin/ratings";

# open file, lock it, and go to last line
open(RFILE,">>$Directory/$File") or die "Could not open file";
flock(RFILE,2);
seek(RFILE,0,2);

# print score and divider ":"
print RFILE "$Rating:";

# PART 3 PERL SCRIPT READS ALL SCORES FROM FILE INTO VARIABLE THEN FINDS TOTAL VOTES BY FINDING LENGTH OF DATA IN FILE AND DIVIDING BY 2 THEN SPLITS BY DIVIDER AND (PROBLEM AREA) ADDS EM ALL UP THEN DIVIDES BY TOTAL VOTES TO FIND AVG

# then read contents into variable
@data = <RFILE>;

# close file
close(RFILE);

# find length of file then divide by 2 to account for dividers ":"
$votetotal = (length(@data)/2);

# then the idea is to split @data on ":" which is the divider printed in the first half of the # file
(@vote) = split(/:/, @data);

# HELP NEEDED HERE next i would add up all the scores as they are all in their own variables but i dont know how since i cant predict how many votes there will be
$scoretotal=?

# find avg score by dividing num of votes by total points
if ($votetotal eq "0") {$avgscore = "NA";}
else { $avgscore = ($scoretotal / $votetotal) ;}

# 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 "</BODY></HTML>";

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives