Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to sort the results of a poll

Status
Not open for further replies.

BarisEren

Programmer
Joined
Jul 14, 2001
Messages
4
Location
DE
I'm very new to Perl and I modified a poll script in Perl. Everything works great so far. The only thing I would like to change is the poll results should be sorted according to votes. The variable for this is $votes. I would like to have the answers with most votes at top.

Can anyone help me please how to modify the code below which is responsible for the output of the poll results?

Cordially,
Baris


Code:
sub results {
        foreach $line(@data) {
                ($topic, $num, $others) = split(/\|/, $line);                
                if ($num eq $FORM{'topic'}) { last }
        }
        print &quot;<html><head><title>$topic :: Results</title></head>\n&quot;;        
        print &quot;<body bgcolor=\&quot;#$bkgdc\&quot; TEXT=\&quot;#$textc\&quot; link=\&quot;#$linkc\&quot; vlink=\&quot;#$vlinkc\&quot;>\n&quot;;
        &sig;
        print &quot;<h1><font color=\&quot;#ffffff\&quot;>$topic</font></h1>\n&quot;;
        print &quot;<center><table bgcolor=\&quot;#333333\&quot; width=\&quot;700\&quot; border=\&quot;0\&quot;><tr><td>\n&quot;;
        print &quot;<table width=100% cols=\&quot;5\&quot;><tr>&quot;;
        print &quot;<td width=\&quot;25%\&quot;> </td><td width=5% align=right ><b>Oy</b></td>\n&quot;;
        print &quot;<td width=10% align=right><b>%</b></td>\n&quot;;
print &quot;<td width=3%>    </td><td width=60%>\n&quot;;
        print &quot;<center><b>Cevap</b></center></td></tr>\n&quot;;
        open (FILE, &quot;$data_path/ch$FORM{'topic'}.txt&quot;);
        @lines = <FILE>;
        close(FILE);
        $i = 0;
        $maxvote=0;
        foreach $line(@lines) {
                ($topic,$votes,$num) = split(/\|/, $line);
                $i = $i + $votes;
                if ($votes>$maxvote) {$maxvote=$votes}
        }
        foreach $line(@lines) {
                ($topic,$votes,$num) = split(/\|/, $line);
                if ($i > 0) { 
                    $percent = sprintf(&quot;%.2f&quot;,100 * ($votes / $i)); 
                    $wide=int(100* $votes / $maxvote);
                    $wideetc=100- $wide;
                    }
                else { $percent = &quot;NA&quot;; }
                print &quot;<tr>&quot;;
                if($votes == 0) {
                    print &quot;<td width=25%> </td>&quot;;
                    }
                else {
#                    print &quot;<td width=25% align=right><i><img src=\&quot;$colorbar\&quot; BORDER=0 HEIGHT=12 WIDTH=$wide%></i></td>&quot;;
                    print &quot;\n<TD width=25%><table width=100% border=0><tr>&quot;;
                    print &quot;<td width=$widerem% bgcolor=#$bkgdc> </td>&quot;;
                    print &quot;<td width=$wide% bgcolor=#$linkc> </td>&quot;;
                    print &quot;</tr></table></td>&quot;;
                    }
                print &quot;<td width=5% align=right><i><b><font color=\&quot;#ffffff\&quot; size=\&quot;3\&quot;>$votes</font></b></i></td>&quot;;
                print &quot;<td width=5% align=right><i><font size=\&quot;2\&quot;>$percent</font></i></td>&quot;;
print &quot;<td width=3%>    </td>\n&quot;;

                print &quot;<td width=60%><font size=\&quot;2\&quot;>$topic</font></td></tr>\n&quot;;
        }
        print &quot;</table><center><form method=\&quot;POST\&quot;><input type=\&quot;submit\&quot; value=\&quot; Anasayfaya Geri\&quot;>\n&quot;;
        print &quot;<input type=\&quot;hidden\&quot; name=\&quot;pagename\&quot; value=\&quot;$pagename\&quot;>\n&quot;;
        print &quot;<input type=\&quot;hidden\&quot; name=\&quot;userdir\&quot; value=\&quot;$userdir\&quot;>\n&quot;;

        print &quot;</form></center></td></tr></table></center>\n&quot;;
        &sig;
        print &quot;</body></html>\n&quot;;
        exit;
}
 
Try this, i don't know if it's right or not i just noticed in quick.


foreach $line sort(ascend(@lines) {
($topic,$votes,$num) = split(/\|/, $line);


sub ascend
{
$a <=> $b;
} -Aaron
 
Thanks for the fast reply Aaron.

I tried


Code:
    foreach $line sort(ascend(@lines) {
                ($topic,$votes,$num) = split(/\|/, $line)
        }

and I added the function ascend outside but it didn't work. I hope anyone else can help me.
 
foreach $line(sort(ascend(@lines))) {
($topic,$votes,$num) = split(/\|/, $line)
}


This code doesn't give an error, but the result is still the same ; (
(I mean nothing is sorted.)
 
ok, here's another one: (don't know it could be used for numerical. try it anyway)

foreach $line (sort(@lines)) {
($topic,$votes,$num) = split(/\|/, $line)
} -Aaron
 
I tried it Aaron. It doesn't give an error but the result is still not sorted. ; (

But thanks for your help.
 
Try AaronGeorge's original suggestion, but change the sort subroutine ascend to this:
Code:
sub ascend {
   my($topic_a, $votes_a, $num_a) = split(/\|/, $a);
   my($topic_b, $votes_b, $num_b) = split(/\|/, $b);
   return $votes_b <=> $votes_a;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top