Newbie, split function, and counter 
Author Message
 Newbie, split function, and counter

Been working on this for HOURS  -  continual errors on the split
function and the counter lines (after making every change imaginable)
, any insight would be appreciated.

#!/usr/bin/perl

#classelection.cgi - saves form data to a file, creates a dynamic Web
page

#that displays a message and election results

print "Content-type:text/html\n\n";

use CGI qw(:standard);

use strict;

#declare variables



#assign input item to a variable

$election = param( 'election' );

#save form data to file

open(OUTFILE, ">>", "classelection.txt")

        or die "Error opening classelection.txt. $!, stopped";

print OUTFILE "$election\n";

close(OUTFILE);

#calculate election results

open(INFILE, "<", "classelection.txt")

        or die "Error opening classelection.txt. $!, stopped";

close(INFILE);


{

        chomp($vote);

        $election = split(/,/, $vote);

        $count[$election] = $count[$election] + 1;

Quote:
}

#generate HTML results

print "<HTML><HEAD><TITLE> Jefferson High School Class President
Election</TITLE></HEAD>\n";

print "<BODY>\n";

print "<CENTER><H2>Thank you for voting for class president</H2>\n";

print "<TABLE>\n";

print "<TR><TD>Jeff Stone</TD>           <TD>$count[0]</TD></TR>\n";

print "<TR><TD>Sheima Nadkarni</TD>      <TD>$count[1]</TD></TR>\n";

print "<TR><TD>Sam Perez</TD>            <TD>$count[2]</TD></TR>\n";

print "</TABLE>\n";

print "</CENTER></BODY></HTML>\n";



Sat, 14 May 2005 03:27:31 GMT  
 Newbie, split function, and counter


Quote:
> Been working on this for HOURS  -  continual errors on the split
> function and the counter lines (after making every change imaginable)
> , any insight would be appreciated.


> open(INFILE, "<", "classelection.txt")
> or die "Error opening classelection.txt. $!, stopped";

> close(INFILE);


> {
> chomp($vote);
> $election = split(/,/, $vote);
> $count[$election] = $count[$election] + 1;
> }

You don't provide us with enough information on the strings coming in from

problem with your use of the split function.  Are the incoming strings
comma-delimited data records?


Sat, 14 May 2005 03:46:10 GMT  
 Newbie, split function, and counter


Quote:
> Been working on this for HOURS  -  continual errors on the split
> function and the counter lines (after making every change imaginable)
> , any insight would be appreciated.



[Lines removed]

Quote:

> $election = split(/,/, $vote);

> $count[$election] = $count[$election] + 1;

A sample input file would really help to understand
what you are trying to do.  Assuming the input lines
are of the form:

foo,bar,baz

$election will be 3 for three fields found.  Your array
$count is zero based, so you will be incrementing
the fourth entry.  Even with this fixed, the file formats
that I can imagine that would make the code do the
right thing are strange.

== Rob ==



Sat, 14 May 2005 03:54:29 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to use split function to split on a backslash

2. Problem with join function (and split function)

3. Counter or Pause function in Perl?

4. hash and counter - newbie

5. newbie....and counters..

6. Newbie: counter sometimes resets

7. Newbie: graphic counter:shes-a no work!

8. using split function twice on same line

9. Split function

10. Using SPLIT function with a Period

11. Using a period as a delimiter in the split() function

12. split function that handles quoting..

 

 
Powered by phpBB® Forum Software