Hash Sorting

B

Brad

I have a hash like this:

$HASH{'key'}{'date'} = some date
$HASH{'key'}{'status'} = some status

I'd like to sort and display this by date:

key | date | status

I cannot seem to find any references to sorting this type of hash in this
way. I need some assistance finding some documentation, or a copy/paste of
the code for this type of sort.

Thanks for any help,
Brad
 
M

Mark Clements

I have a hash like this:

$HASH{'key'}{'date'} = some date
$HASH{'key'}{'status'} = some status

I'd like to sort and display this by date:

key | date | status

I cannot seem to find any references to sorting this type of hash in this
way. I need some assistance finding some documentation, or a copy/paste of
the code for this type of sort.
You can do it with a double map and sort.

Google for

"schwarzian transform" perl

Mark
 
B

Brad

You can do it with a double map and sort.
Google for

"schwarzian transform" perl

Mark

Thanks Mark... kept searching, after my post, and I found the answer:

foreach my $spc ( sort { $hoh{$a}->{'cpu'} <=> $hoh{$b}->{'cpu'} } keys
%hoh) {
print $spc, " has got ", ${hoh}{$spc}{cpu}, "\n";
}
 
A

Anno Siegel

Mark Clements said:
You can do it with a double map and sort.

Google for

"schwarzian transform" perl

Why do you think that's useful?

A Schwartzian transform is an efficiency hack that saves time when the
extraction of sort keys from the elements is expensive. It doesn't
facilitate any particular ordering, you'll have to do that either way.

Since the sort keys are available through hash access, there isn't much
a Schwartzian can gain in this case.

Anno
 
M

Mark Clements

Why do you think that's useful?
It demonstrates passing different comparison operations to sort, and the
use of multiple maps, though I didn't realise that the Schwarzian transform
was specific to non-prekeyed data.
A Schwartzian transform is an efficiency hack that saves time when the
extraction of sort keys from the elements is expensive. It doesn't
facilitate any particular ordering, you'll have to do that either way.
Since the sort keys are available through hash access, there isn't much
a Schwartzian can gain in this case.
Fair enough, but the suggestion was intended more as a learning exercise
than as a "do it this way".

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top