Gossamer Forum
Home : General : Perl Programming :

Hash Sorting

Quote Reply
Hash Sorting
Not much annoys me about perl but this is one. Why oh why do hashes stop sorting at key 9?

Code:
my $hash1 = { 1 => 'a',
4 => 'd',
2 => 'b',
3 => 'c'
};
my $hash2 = { 1 => 'a',
4 => 'd',
2 => 'b',
3 => 'c',
5 => 'e',
7 => 'g',
6 => 'f',
8 => 'h',
9 => 'i'
};
my $hash3 = { 1 => 'a',
4 => 'd',
2 => 'b',
3 => 'c',
5 => 'e',
7 => 'g',
6 => 'f',
8 => 'h',
9 => 'i',
11 => 'k',
10 => 'j'
};

print "Content-type: text/html\n\n";
print join ' - ', %$hash1;
print "<br>";
print join ' - ', %$hash2;
print "<br>";
print join ' - ', %$hash3;

The output:

Code:
1 - a - 2 - b - 3 - c - 4 - d
1 - a - 2 - b - 3 - c - 4 - d - 5 - e - 6 - f - 7 - g - 8 - h - 9 - i
1 - a - 2 - b - 3 - c - 10 - j - 4 - d - 11 - k - 5 - e - 6 - f - 7 - g - 8 - h - 9 - i

Last edited by:

Paul: Aug 22, 2002, 5:37 PM
Quote Reply
Re: [Paul] Hash Sorting In reply to
Try this:
Code:
my $hash4 = { 01 => 'a',
04 => 'd',
02 => 'b',
03 => 'c',
05 => 'e',
07 => 'g',
06 => 'f',
08 => 'h',
09 => 'i',
11 => 'k',
10 => 'j'
};

print "Content-type: text/html\n\n";
print join ' - ', %$hash4;
print "
";

- wil
Quote Reply
Re: [Paul] Hash Sorting In reply to
From the description of the each function:

"Entries are returned in an apparently random order. The actual random order is subject to change in future versions of perl, but it is guaranteed to be in the same order as either the keys or values function would produce on the same (unmodified) hash."

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Hash Sorting In reply to
So is that saying the hash "sorts" in the same order it was created?, so really it isn't sorted at all.
Quote Reply
Re: [Paul] Hash Sorting In reply to
I don't think it's sorted in the order it was created.... just random (sort of).

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Wil] Hash Sorting In reply to
Hehe nice try Wil but that code won't work Wink

You'll have to quote those keys if you want to get past the fatal errors and if you do it still doesn't show anything different to what I said.

Last edited by:

Paul: Aug 23, 2002, 2:40 AM
Quote Reply
Re: [yogi] Hash Sorting In reply to
Blech, that makes no sense to me....silly hashes Smile
Quote Reply
Re: [Paul] Hash Sorting In reply to
Hashes are lookup structures.

It's kind of like a two dimensional array.

First you take the key and reduce it into a number using a "hash function". (one method to do this is to add the ascii values of each char then mod it by the size of your array)

Then you use that number as an index into an array where each element is considered a "bucket" where you throw in your new element.

The reason why it doesn't come out in any perfect order is that when you retrieve the values from the hash, it goes into the first bucket, burps up what it finds there, and then the second bucket... and so on. Due to the nature of perl's hash function, it "seems" like the keys are initially being sorted, but they aren't, it's actually a quirky aftereffect of the algo which doesn't guarentee any such thing.

If you want detailed information on what hashes are, check this out http://ciips.ee.uwa.edu.au/...210/hash_tables.html That URL goes into gory details but you'll be able to implement one of your own after reading it Wink
Quote Reply
Re: [Aki] Hash Sorting In reply to
Uh oh, I sense it is going to take me a while to get my head around that link, thanks, I'll take a look Smile
Quote Reply
Re: [Paul] Hash Sorting In reply to
it's a simple concept, but it's brutal to describe in text. It's best suited in a chalkbustin and hand waving environment. Another way of looking at it, is that hashes are a subprogram on top of an array that converts a string into a number and uses that number to store it in an array. There's a whole lot of complications like "what happens if the number resulted is the same as one before" that that doc talk about. Good luck with it Cool
Quote Reply
Re: [Aki] Hash Sorting In reply to
Thanks, I'll need it :)

On an unrelated note I just realised my $sth object returned an arrayref of column names heh

$sth->{NAME}