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!
@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.
<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>
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?