CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Want to sort the results of a poll

 



Bamya
stranger

Jul 14, 2001, 5:42 PM

Post #1 of 9 (2285 views)
Want to sort the results of a poll Can't Post

Can anyone help me please?

You can read my problem here.

http://yabb.xnull.com/community/?board=scripting&action=display&num=995145362

I hope anyone can help me.

Cordially,
Bamya



mhx
Enthusiast / Moderator

Jul 14, 2001, 11:48 PM

Post #2 of 9 (2278 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

Hi Bamya,

I've applied the neccessary changes to your code that should make the results sorted. The sections that I changed are highlighted in red.

Code
open (FILE, "$data_path/ch$FORM{'topic'}.txt"); 
@lines = sort {$b->[1] <=> $a->[1]}
map [split /\|/], <FILE>;

close(FILE);

$i = 0;
$maxvote=0;

foreach $line (@lines) {
($topic,$votes,$num) = @$line;
$i = $i + $votes;
if ($votes>$maxvote) {$maxvote=$votes}
}

foreach $line (@lines) {
($topic,$votes,$num) = @$line;
if ($i > 0) {
$percent = sprintf("%.2f",100 * ($votes / $i));
$wide=int(100* $votes / $maxvote);
$wideetc=100- $wide;
}

# ...

}

I wasn't able to test it as a whole, but I think it should work. If you run into any trouble, please tell so I can have a detailed look what could be wrong.

FYI: My modification uses an array of arrays to a) speed up processing, because split is rather slow and shouldn't be called unneccessary and b) to make the sort easier. The sorting is done at the very beginning when the lines of the file are read. Each element of the @lines array is now a reference to a three-element array that contains topic, votes and num. To use the array referenced by each line, you have to put the @ in front of the $line. If you need further explanation, feel free to ask.

Hope this helps.

-- Marcus



Bamya
stranger

Jul 15, 2001, 7:55 AM

Post #3 of 9 (2271 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

Marcus,
thanks a lot for your help. I think you are a genius. I tried the code and it's working great!

THANKS, THANKS, THANKS my friend!
(Maybe you could help me modifying the script a little more?)

Best greetings Bamya



mhx
Enthusiast / Moderator

Jul 15, 2001, 8:40 AM

Post #4 of 9 (2270 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

Hi Bamya,

Glad to hear the code worked ;-) But that was more or less standard Perl, it has nothing to do with geniusness.

In Reply To
Maybe you could help me modifying the script a little more?

If there are other things I can help you with, feel free to ask. As a hint, the more input you provide -- pointers to scripts instead of only code snippets, input data to test the code -- the easier it is for people on this forum to help you. But don't forget to point out exactly what you want, a 200-line script without detailed description is also rather worthless.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



Bamya
stranger

Jul 15, 2001, 8:54 AM

Post #5 of 9 (2269 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

Hi Marcus,
can you write me an email please, so that I can send you my question per email?

Here's my address: dj.bamya@gmx.ch



mhx
Enthusiast / Moderator

Jul 15, 2001, 9:18 AM

Post #6 of 9 (2268 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

Just happened...


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



Bamya
stranger

Jul 15, 2001, 9:24 AM

Post #7 of 9 (2267 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

Thanks, soon you'll get an answer!



Jasmine
Administrator / Moderator

Jul 15, 2001, 11:18 AM

Post #8 of 9 (2263 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

In the future, we request that questions aren't moved to private email -- this way, everyone can benefit from moderator or user replies.



Bamya
stranger

Jul 15, 2001, 11:21 AM

Post #9 of 9 (2262 views)
Re: Want to sort the results of a poll [In reply to] Can't Post

Yes, but the question has already been answered and everyone can use my asked question/ answer by Marcus.
Or not?


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives