Bronston has asked for the wisdom of the Perl Monks concerning the following question:

Alright, I've digging around the internet for a couple of days now, and have become nearly disgusted. I am very new to perl so forgive me if there is an obvious answer, but what I need to do is this. I have a website that has a series of polls, all made out of forms. Each poll has ten possible choices, each of which feed into an output file. Then the results are displayed. What I need it to do, is to show only the most voted on result, or really show an image based on the winner, but the image part is pretty simple. The problem here is if I feed it into an array and sort it I lose track of which variable had the most votes, I'm just left with the number of votes. If anyone can offer me some guidence I would be extremely grateful. Thanks B
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

    Without any code or data to work with it's obviously very difficult to provide much help.

    One possible solution might be to package your data into hashrefs, something like:

    { choice => $choice_data, votes => $number_of_votes, ... }

    Add those hashrefs into your array then sort on the number of votes - something like this:

    my @sorted = sort { $a->{votes} <=> $b->{votes} } @unsorted;

    The @sorted array retains all the original information but now it's ordered.

    Please read: How do I post a question effectively?

    -- Ken

      I think I got it figured out. I was more or less unaware of hashes, but that was exactly the direction I needed to go. Thank you very much. B
Re: Perl Sorting +
by marto (Cardinal) on Dec 04, 2010 at 12:08 UTC