PDA

View Full Version : Another Perl question



Canuckkev
March 28th, 2001, 00:31
Okay, my top 10 ranking list script is almost done. I have learned a lot of Perl in the last few days as I have been working on this project. The final task: Create and HTML listing page of all the ratings stored in text files ranked from 1 to 10. So far I have been able to arrange all the data I need into one large %hash . But now I don't know how to organize it so the most highly ranked item is at the top, so when I run a foreach loop, it will process all the results in order.

Have I lost you yet? Hope not. Basically, I need to be able to re-organize a hash so the entries are listed in order by one variable stored in an array in an entry in the hash. (Sense I make no) So I need to be able to rank it from this value: $file_data{'$file'}[2], then relist all the
entries in order from this one value. BTW, this value is a numeric number in the format 9.99.

Okay, I doubt any of you guys(and gals) have understood this much, so please ask questions(that ism if you want to help me) and I will answer them. Thank you very much to anyone who responds.

[Edited by Canuckkev on 03-28-2001 at 01:42 AM]

akersche
March 28th, 2001, 13:15
hope i don't make a fool out of myself, cause it's kind of tricky to get what you want...

would it help, when you use two arrays?
i think of the original array, and an new array, where everything is in the correct order. and in each loop you insert the highest ranked into the first free space of the new array. (and delete it from the old one).

a good idea would to use a loop in a loop.

perhaps you wanna send me your code and i can try to help then...

Greetings
Arno

atlas
March 28th, 2001, 16:03
I'm pretty sure this works:


foreach my $key (sort { $file_data{$b}[2] <=> $file_data{$b}[2] } keys %file_data)
{
# use $key normally
}

Have I mentioned that perl rocks? ;)

-mk
atlascgi.com

Canuckkev
March 29th, 2001, 17:57
Thanks atlas. I've put it in my code, but stupid gcsnet.net is not working, so I can't try it out....yet

Okay, I uploaded it and tried it, didn't quite work.

I don't get my the $key works. Using the foreach loop, does it search through all the entries in the %hash, then store the name of the value entry as $key. Like, does $key = $file_data{'name'}?

Thanks.

Oh, and just to make sure, is it $b<=>$b like you said, or is it $b<=>$a?

Epgs
March 29th, 2001, 19:36
yeah try that and if it doesn't work try what i do in math, guess and check

atlas
March 29th, 2001, 22:31
Originally posted by Canuckkev
Thanks atlas. I've put it in my code, but stupid gcsnet.net is not working, so I can't try it out....yet

Okay, I uploaded it and tried it, didn't quite work.

I don't get my the $key works. Using the foreach loop, does it search through all the entries in the %hash, then store the name of the value entry as $key. Like, does $key = $file_data{'name'}?


$key is then the actual key -- like 'name' in your example. To get the value do:


$file_data{$key}


The foreach iterates through each key value (and in this case it's doing them in a sorted order).



Oh, and just to make sure, is it $b<=>$b like you said, or is it $b<=>$a?


Yeah, that's an error: it should be $b <=> $a

-mk
atlascgi.com