CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
sorting hash of hash of arrays

 



taylor_77
New User

Oct 28, 2010, 9:51 AM

Post #1 of 4 (862 views)
sorting hash of hash of arrays Can't Post

I have the following data structure, a hash called %data
So its a hash of hash of arrays.

Its hostname -> retransmits -> array of usernames.

I'd like to be able to sort by the second key, the numbers 2139, 11, 5.



Code
$VAR1 = { 
'Server1' => {
'2139' => [
'jsmith,bsmith'
]
},
'Server2' => {
'11' => [
'rsmith'
]
},
'Server3' => {
'5' => [
'asmith,zsmith'
]
}
};


I tried:


Code
foreach $host (sort (keys(%data))) { 
foreach $total_retrans (sort { $data{$host}{$b} <=> $data{$host}{$a} } keys %{ $data{$host} } ) {
print "$host, $total_retrans, $data{$host}{$total_retrans}[0]\n";
}
}


This prints everything just fine, but not sorted by the retransmits.

Thanks in advance,
Taylor


BillKSmith
Veteran

Oct 28, 2010, 12:16 PM

Post #2 of 4 (856 views)
Re: [taylor_77] sorting hash of hash of arrays [In reply to] Can't Post

One solution is to create an index consisting of pairs of keys. Sort the index, first by total_trans and then by host. Use the index to control the print.


Code
use strict; 
use warnings;
my %data = (
'Server1' => { '2139' => ['jsmith,bsmith'] },
'Server2' => { '11' => ['rsmith'] },
'Server3' => { '5' => ['asmith,zsmith'] }
);

my @index;
foreach my $host (keys %data) {
foreach my $total_retrans (keys %{$data{$host}}) {
push @index, [$host, $total_retrans];
}
}
@index = sort {
$a->[1] <=> $b->[1]
||
$a->[0] cmp $b->[0]
} @index;

foreach (@index) {
my ($host, $total_retrans) = @$_;
print "$host, $total_retrans, $data{$host}{$total_retrans}[0]\n";
}

Good Luck,
Bill


taylor_77
New User

Oct 28, 2010, 1:00 PM

Post #3 of 4 (855 views)
Re: [BillKSmith] sorting hash of hash of arrays [In reply to] Can't Post

Thanks Bill, that worked.

One question on the sort part. When you specify

Code
    $a->[1] <=> $b->[1]  
||
$a->[0] cmp $b->[0]


Is this saying, sort numerically on array index [1], or alphabetically on array index [0], meaning if the numerical values are equal then we will sort on server name?


BillKSmith
Veteran

Oct 28, 2010, 2:01 PM

Post #4 of 4 (851 views)
Re: [taylor_77] sorting hash of hash of arrays [In reply to] Can't Post


Quote
Is this saying, sort numerically on array index [1], or alphabetically on array index [0], meaning if the numerical values are equal then we will sort on server name?



The short answer is "yes".

Consider the operator || (logical or). Logically it returns true if either (or both) of its arguments are true (non-zero). In the perl implementation of this logic, if the first argument is non-zero (true), its value is returned. The second argument is not even evaluated. In the other case where the first argument is zero (false), the second argument is evaluated and its value is returned.

In our expression, the first expression is zero only if the two numbers are numerically equal. In that case, the two strings are compared lexically.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives