Bronston has asked for the wisdom of the Perl Monks concerning the following question:
poll.html <form action="poll.cgi" method="POST"> Poll number 1.<br> <input type="radio" name="pick" value="1">1<br> <input type="radio" name="pick" value="2">2<br> <input type="radio" name="pick" value="3">3<br> <input type="radio" name="pick" value="4">4<br> <input type="radio" name="pick" value="5">5<br> <input type="radio" name="pick" value="6">6<br> <input type="radio" name="pick" value="7">7<br> <input type="radio" name="pick" value="8">8<br> <input type="radio" name="pick" value="9">9<br> <input type="radio" name="pick" value="10">10<br> <input type="submit" value="Vote"> </form> <a href="results.cgi">View Results</a><br> poll.cgi #!i:/perl/bin/perl use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; use Fcntl qw(:flock :seek); my $outfile = "poll.out"; if (param('pick')) { open(OUT, ">>$outfile") or &dienice("Couldn't open $outfile: $!"); flock(OUT, LOCK_EX); # set an exclusive lock seek(OUT, 0, SEEK_END); # then seek the end of file print OUT param('pick'),"\n"; close(OUT); } else { &dienice("You didn't pick anything!"); } print redirect"results.cgi"; sub dienice { my($msg) = @_; print header; print start_html("Error"); print h2("Error"); print $msg; print end_html; exit; } results.cgi #!i:/perl/bin/perl use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Fcntl qw(:flock :seek); my $outfile = "poll.out"; print header; print start_html("Results"); open(IN, "$outfile") or &dienice("Couldn't open $outfile: $!"); flock(IN, LOCK_SH); seek(IN, 0, SEEK_SET); my($total_votes, $results); foreach my $i ("10", "1", "2", "3", "4", "5", "6", "7", "8", "9" +) {$results{$i} = 0;} while (my $rec = <IN>) { chomp($rec); $total_votes = $total_votes + 1; $results{$rec} = $results{$rec} + 1; } close(IN); print <<END; <b>Poll number 1 </b><br> <table border=0 width=50%> <tr> <td>1</td> <td>$results{10} votes</td> </tr> <tr> <td>2</td> <td>$results{1} votes</td> </tr> <tr> <td>3</td> <td>$results{2} votes</td> </tr> <tr> <td>4</td> <td>$results{3} votes</td> </tr> <tr> <td>5</td> <td>$results{4} votes</td> </tr> <tr> <td>6</td> <td>$results{5} votes</td> </tr> <tr> <td>7</td> <td>$results{6} votes</td> </tr> <tr> <td>8</td> <td>$results{7} votes</td> </tr> <tr> <td>9</td> <td>$results{8} votes</td> </tr> <tr> <td>10</td> <td>$results{9} votes</td> </tr> </table> <p> $total_votes votes total </p> END print end_html; sub dienice { my($msg) = @_; print h2("Error"); print $msg; print end_html; exit; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Sorting +
by kcott (Archbishop) on Dec 04, 2010 at 12:24 UTC | |
by Bronston (Novice) on Dec 04, 2010 at 12:29 UTC | |
|
Re: Perl Sorting +
by marto (Cardinal) on Dec 04, 2010 at 12:08 UTC |