
japhy
Enthusiast
Aug 9, 2000, 5:06 AM
Post #9 of 10
(2955 views)
|
You don't seem to be understanding the chief issue here, Wil. Hashes CAN NOT be sorted. There is no such thing in Perl as a sorted hash -- the ONLY thing you can do is iterate over the keys in a specific order. Example: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> %hash = qw( mine yours his hers ours theirs Jack Jill ); for (sort keys %hash) { print "$_ and $hash{$_}\n"; } # this prints: # Jack and Jill # his and hers # mine and yours # ours and theirs for (sort { $hash{$a} cmp $hash{$b} } keys %hash) { print "$_ and $hash{$_}\n"; } # this prints: # Jack and Jill # his and hers # ours and theirs # mine and yours </pre><HR></BLOCKQUOTE> The first sorted list of keys is the keys in ASCIIbetical order. The second sorted list of keys is the keys based on the VALUES in ASCIIbetical order. Notice: J, h, m, o, in the first list (the starting letters of the KEYS) vs. J, h, t, y, in the second list (the starting letters of the VALUES). ------------------ Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher
|