Sorting hash of hashes

2005-10-06 Thread Rose, Jeff
I have been trying to sort a hash but I cannot figure it out for the
life of me

 

The hash is in the form:

my %message {

messageid {  From=

To=  

To_Num= 'Int'

Sub_IP =

Subject =

}

}

 

Right now my sort is looking like

foreach (sort {$message{$_}-{'To_Num'}[$a] =
$message{$_}-{'To_Num'}[$b]} keys (%message)) {

}

 

I know it doesn't work, and I'm not sure if that's the proper way to do
it, maybe you guys could give me a little insight

 

On second thought $_ is not initialized yet in the foreach loop,
possibly: 

foreach (sort {$message{$a} cmp $message{$b}} keys (%message)) {

foreach (sort {$message{$_}-{'To_Num'}[$a] =
$message{$_}-{'To_Num'}[$b]} keys (%message)) {

}

}

 

 

Thanks,

 



Re: Sorting hash of hashes

2005-10-06 Thread Jeff 'japhy' Pinyan

On Oct 6, Rose, Jeff said:


I have been trying to sort a hash but I cannot figure it out for the
life of me


I've fixed a bit of the formatting below...


my %message = (
  $messageid = {
From= $from,
To  = $to,
To_Num  = $num,
Sub_IP  = $ip,
Subject = $subject,
  },
  ...
)



foreach (sort {$message{$_}-{'To_Num'}[$a] =
$message{$_}-{'To_Num'}[$b]} keys (%message)) {
}



foreach (sort {$message{$a} cmp $message{$b}} keys (%message)) {



foreach (sort {$message{$_}-{'To_Num'}[$a] =
$message{$_}-{'To_Num'}[$b]} keys (%message)) {


You keep putting $a and $b all the way at the end of the things you're 
sorting.  I don't think you actually know what $a and $b are.  $a and $b 
represent two elements of the list you're sorting -- this means they're 
two keys from %message.  This means the ONLY place it makes sense for them 
to be is $message{$a} and $message{$b}.  To sort my the 'To_Num' field, 
then, you would do:


  sort { $message{$a}{To_Num} = $message{$b}{To_Num} } keys %message

--
Jeff japhy Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response