PDA

View Full Version : Perl help...setting counters



feiticeria
September 24th, 2006, 09:46 PM
To all those Perl geniuses out there, I am need of your help!
I am in charge of a university club website and the previous person running the show made a Perl based system to get people's name, year & email. This program is used for people to sign-up for events help by the club.

Now the problem, certain events have seating limits and I would like the program to stop people from signing-up when the limit is reached. So...i need:
- to create a variable to set the seating limit,
- another variable to act as a counter
- and when the counter has reached the limit variable, possibly have program redirect the person to a webpage (possibly using a "do while" loop).

Here is the current code:

#!/usr/bin/perl -wT
use CGI qw(:standard);


my $mail = param('EMAIL'); #in form name@host

if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-\_]+$/ ) #characters allowed on name: 0-9a-Z-._ on host: #0-9a-Z-._ on between: @
{
print "Location: http://stw.ryerson.ca/~acc/event-fail.html\n\n";
}

elsif ( $mail !~ /\.([a-zA-Z]{2,3})$/ ) { #host must end with '.' plus 2 or 3 alpha for #TopLevelDomain (MUST be modified in future!)
print "Location: http://stw.ryerson.ca/~acc/event-fail.html\n\n";
}

else
{
# now write (append) to the file
open(OUT, ">>record2.txt") or &dienice("Could not open output file: $!");

foreach my $p (param()) {
print OUT "|", param($p);
}
print OUT "\n";
close(OUT);

print "Location: http://stw.ryerson.ca/~acc/event-success.html\n\n";
}


I personally don't know perl, but I have some experience with Java, so I have an idea of how to fix my problem, just not too sure how to implement it with Perl.

Your help would be greatly appreciate. Even if you can tell me how to create a integer variable, it will go a long way.

bwh2
September 24th, 2006, 10:26 PM
#!/usr/bin/perl -wT
use CGI qw(:standard);

# file that gets written to
my $outputFile = "record2.txt";

# temporary line count for the output file (pre-write)
my $tmpLineCount = 0;

# count the number of lines in the output file
open ( TMP_IN, $outputFile );
while( <TMP_IN> ) {
$tmpLineCount++;
}

# close that output filei
close (TMP_IN);

# establish limit
my $limit = 20;

# test if the limit has been reached
if( $tmpLineCount >= $limit ) {
print "This event has already reached the the limit.\n";
}
else {
my $mail = param('EMAIL'); #in form name@host

if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-\_]+$/ ) #characters allowed on name: 0-9a-Z-._ on host: #0-9a-Z-._ on between: @
{
# print to screen
print "Location: http://stw.ryerson.ca/~acc/event-fail.html\n\n";
}

elsif ( $mail !~ /\.([a-zA-Z]{2,3})$/ ) { #host must end with '.' plus 2 or 3 alpha for #TopLevelDomain (MUST be modified in future!)
# print to screen
print "Location: http://stw.ryerson.ca/~acc/event-fail.html\n\n";
}

else
{
# now write (append) to the file
open(OUT, ">>".$outputFile) or &dienice("Could not open output file: $!");

# print out each parameter
foreach my $p (param()) {
print OUT "|", param($p);
}

# print a line break
print OUT "\n";

# close the output file
close(OUT);

# print to screeen
print "Location: http://stw.ryerson.ca/~acc/event-success.html\n\n";
}
}
that should work.

feiticeria
October 1st, 2006, 03:12 AM
Thank you bwh2 for your help the code works great!
I would like to have the counter "LineCount" kept as record on a seperate .txt file as I would like an easy way to keep up with the count. I have been attempting to figure this out myself, following whats been done to the coding, but its not working. This is what I've tried, I put the following code on the bottom of the .pl file, after the above coding:


# count the number of lines in the output file
open (COUNT, ">>counter.txt") or &dienice("Could not open output file: $!");
print COUNT $tmpLineCount;
close (COUNT);

Can someone please tell me what I've doing wrong in the above? Thank you again.

bwh2
October 1st, 2006, 06:47 PM
i would advise against holding the count in a separate text file. basically you're adding a step to the process without really adding any real benefit.