>Sharon writes..
>
>>I have a hash of hashes.
>>The main key is the order number.
>>Then that order has a hash of info, for which the key/values are shown
>>below.
>>
>>I have the retrieve and display code working. What I really want to do
>>is sort by the DATE. I figured out how to sort by the order number but
>>can't figure out how to make the top sort be for the 'inner hash'
>
>All hail Randal, this is known as a Schwartzian Transform after Randal
>Schwartz who came up with the algorithm/idiom. The idea is that you
>sandwich the sort inside two maps which alter the keys that sort has
>available to it and the returns those keys to their original state. I
>don't know how your date is structured, so I don't know how to do a sort
>on it, so the following example just shows a numerical sort, but if your
>dates are of some other format then you are probably best to write a
>small sub to do that comparison for you and use it in the sort:
>
> for my $key ( map { $_->[1] }
> sort { $a->[0] <=> $b->[0] }
> map { [ $hash{$_}{date}, $_ ] }
> keys %hash
> )
> {
> # do blah with sorted $key
> }
>
>I just wanted to add something about your syntax here:
>
>>%hash = (%hash,
>> $ordernum => {
>> date => @data[1],
>> submitby => @data[2],
>> mgmt => $mgt,
>> title => @data[5],
>> location => @data[9],
>> },
>>);
>
>Those @data[x] values above are array slices, they are better expressed
>as normal scalars: $data[1] etc. Ie. replace the @ with a $ when
>expressing single values like you are.
>
>Had you put this in your code:
>
> use warnings;
>
>To enable warnings, then Perl would have suggested that to you.
>
>--
> Jason King
Maybe everyone else understands how Randal's code works but I assume he won
some award because the average guy wouldn't have though of the method. And
I don't like relying on tricks... OK maybe its not a trick but when its
magic to me its a trick.
So an alternative that I am sure is much slower is to create a new hash
with keys of the dates of sales... humm works OK until you have two sales
the same day. OK add the
order number to the date to create a unique key.
code:
use warnings;
use strict;
my %order;
my %list;
$order{1}{date}="20030215";
$order{1}{sub}="Salesman5";
$order{1}{mgr}="manager1";
$order{2}{date}="20030212";
$order{2}{sub}="Salesman4";
$order{2}{mgr}="manager2";
$order{3}{date}="20030213";
$order{3}{sub}="Salesman3";
$order{3}{mgr}="manager3";
$order{4}{date}="20030209";
$order{4}{sub}="Salesman2";
$order{4}{mgr}="manager4";
$order{5}{date}="20030209";
$order{5}{sub}="Salesman1";
$order{5}{mgr}="manager5";
foreach my $ordernum ( keys %order ) {
$list{$order{$ordernum}{date}."-".$ordernum}= $ordernum;
}
foreach my $bydate ( sort keys %list ) {
print "Order#: $list{$bydate}\n";
print "Date : $order{$list{$bydate}}{date}\n";
print "SalesM: $order{$list{$bydate}}{sub}\n";
print "Mgr : $order{$list{$bydate}}{mgr}\n\n";
}
produces:
Order#: 4
Date : 20030209
SalesM: Salesman2
Mgr : manager4
Order#: 5
Date : 20030209
SalesM: Salesman1
Mgr : manager5
Order#: 2
Date : 20030212
SalesM: Salesman4
Mgr : manager2
Order#: 3
Date : 20030213
SalesM: Salesman3
Mgr : manager3
Order#: 1
Date : 20030215
SalesM: Salesman5
Mgr : manager1
__end
Basil
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs