Forum Moderators: coopster & phranque

Message Too Old, No Replies

Sorting a hash of hashes

I need a plain, vanilla example that doesn't use PRINT!

         

AWildman

5:47 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



I have scoured the web for an example of how to sort a hash of hashes, and, I'm probably being a bonehead, but I'm not finding one that simply sorts and doesn't require that something else be done. They all are of the format

foreach my $userInfo (sort {$db{$a}->{$dbIndex} cmp $db{$b}->{$dbIndex}} keys %db)
{print "$userInfo\n";}

That's all fine and dandy but I don't want to print the hash, I just want it sorted. Am I missing something obvious like replacing the print statement with code to assign $userInfo to a new hash? If so, could someone show me what it might look like?

My hash is created as follows:

$db{$dbIndex} =
{
"Name" => $dbName,
"ComputerPlatform" => $dbComputerPlatform,
"CableType" => $dbCableType,
"School" => $dbSchool,
"Address" => $dbAddress,
"Position" => $dbPosition,
"Phone" => $dbPhone,
"Email" => $dbEmail,
"TimesAccessed" => $dbTimesAccessed
};

What I want is to sort %db by %dbIndex.
I'm new to Perl and the whole hash thing. I'm trying my darndest though! :)

I'm not SO new that I don't know about Schwartian Transforms so don't worry about showing me how to go that route for fear that you'll totally lose me!

VectorJ

8:09 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



I may be misunderstanding what you want to do, but I think this would work (assuming dbIndex is numeric):


@sorted = sort { $a <=> $b } keys %db;

From what I understand, you're sorting by the %db key, so it doesn't matter that %db is actually a hash of hashes. The @sorted array will contain the keys for the %db hash in ascending sorted order, and you can step through them using a foreach loop.

AWildman

8:21 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



I've seen that too, but now there is an array instead of a hash unless I'm misunderstanding something. I want to begin and end with a hash.

<edit>I'm a bozo. Queen of telling tech support customers to read everything and then I don't listen to my own advice! Sorry, but I didn't read all the important stuff after the code. I don't know how to go through the array and put stuff back into the hash. </edit>

VectorJ

8:24 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



A hash can't be sorted and stay a hash; hashes do not guarantee any particular order of their elements. You have to keep a list of the proper order of the keys in an array, then use that to output the hash in the order you want.

VectorJ

8:30 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



Something like this may work for you (after the sort):


foreach $dbIndex(@sorted) {
$db{$dbIndex}{'Name'} = 'hoodly';
$db{$dbIndex}{'ComputerPlatorm'} = 'doodly';
}

Or whatever it is that you want to do with each record in order.

AWildman

8:43 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



Well, actually, I don't want to do anything with the hash elements. I just wanted to sort the hash so that when I'm looking through it for something, it doesn't take so long. But, if that is not something I can do with a hash, so be it.

I appreciate the help! Thanks much.

timster

1:58 pm on Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just wanted to sort the hash so that when I'm looking through it for something, it doesn't take so long.

A hash isn't "sorted" in any way recognizable to us mortals, but it is indexed, and should be fast.

Here's a description of what really's going on inside a hash:
[perl.com...]

If you're having real slowness, maybe there's some other problem -- do you want to give some more detail?

AWildman

4:12 pm on Jun 17, 2004 (gmt 0)

10+ Year Member



I haven't had any performance problems, but I assumed that I would if the database I'm using gets large enough. I'm sticking info from a flat-text file into a hash to search for user info. I just thought I'd nip it in the bud if there was going to be a problem. Thanks for the link!