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: Intermediate: Re: [TX_Jimmy] Misbehaving Hash Sorting: Edit Log



7stud
Enthusiast

Nov 23, 2009, 10:40 AM


Views: 2519
Re: [TX_Jimmy] Misbehaving Hash Sorting

Hi,

For some reason, your sort function sees the same value for %nhash (the first value) every time through the loop. Therefore, it thinks the key/value pairs are:

ap1=30
ap2=45
ap3=16
etc.

and you end up sorting the keys by those values. As a result, the keys always get sorted in this order:

ap2
ap1
ap3

Subsequently, when you print out the values associated with each of the sorted keys, you get the values from the current %nhash. If you add the following to your sort function, you will see what I mean:


Code
	sub by_count { 
print "$a=$nhash{$a}, $b=$nhash{$b}\n";
$nhash{$b} <=> $nhash{$a}
};



I can't explain why that is happening, though.


(This post was edited by 7stud on Nov 23, 2009, 11:22 AM)


Edit Log:
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 10:42 AM
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 10:42 AM
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 10:48 AM
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 10:49 AM
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 10:49 AM
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 11:18 AM
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 11:19 AM
Post edited by 7stud (Enthusiast) on Nov 23, 2009, 11:22 AM


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

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