Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Sorting HoH on 2nd level key?

by Anonymous Monk
on Jul 24, 2008 at 03:47 UTC ( [id://699762]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I can't seem to figure out how to sort on the 'priority' part of my HoH. Here's a short example of what I have tried:
#!/usr/bin/perl -w use strict; my %tmp = ( 'hostX1-1-l.example.com' => { 'priority' => { 'IS-ABC' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostC1-3-l.example.com' => { 'priority' => { 'IS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostc1-3-l.example.com' => { 'priority' => { 'OS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostC1-9-l.example.com' => { 'priority' => { 'XS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostH1-2-l.example.com' => { 'priority' => { 'IS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostQ1-3-l.example.com' => { 'priority' => { 'IS-ABC' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, ); sub sortBySite { my $site_a = $tmp{$a}{'priority'}; my $site_b = $tmp{$b}{'priority'}; return $site_a <=> $site_b; } #foreach my $host ( sort { $tmp{$a}{'priority'} <=> $tmp{$b}{'priority +'} } sort keys %tmp ) { #foreach my $host ( sort keys %tmp ) { foreach my $host ( sort sortBySite keys %tmp ) { for my $site ( keys %{ $tmp{$host}{'priority'}} ) { print "$host->$site is $tmp{$host}{'priority'}{$site}\ +n"; } }
The output I get is this:
hostX1-1-l.example.com->IS-ABC is 3 hostC1-3-l.example.com->IS-XYZ is 3 hostc1-3-l.example.com->OS-XYZ is 3 hostC1-9-l.example.com->XS-XYZ is 3 hostH1-2-l.example.com->IS-XYZ is 3 hostQ1-3-l.example.com->IS-ABC is 3
What I am try to get is this:
hostQ1-3-l.example.com->IS-ABC is 3 hostX1-1-l.example.com->IS-ABC is 3 hostC1-3-l.example.com->IS-XYZ is 3 hostH1-2-l.example.com->IS-XYZ is 3 hostc1-3-l.example.com->OS-XYZ is 3 hostC1-9-l.example.com->XS-XYZ is 3
Thanks

Replies are listed 'Best First'.
Re: Sorting HoH on 2nd level key?
by pc88mxer (Vicar) on Jul 24, 2008 at 03:55 UTC
    Your priority values are hashes. So how do you want to compare {'IS-ABC' => '3'} with {'IS-XYZ' => '3'}? Do you compare IS-ABC with IS-XYZ alphabetically, and what if they are equal?

    From your desired output here's something that I think will work:

    sub sortBySite { my ($site_a) = keys %{$tmp{$a}{'priority'}}; my ($site_b) = keys %{$tmp{$b}{'priority'}}; return $site_a cmp $site_b; }

    Also, does your data really need to be stored as a HoH? Would this simpler structure work for you?

    'hostX1-1-l.example.com' => { priority => 'IS-ABC', priority_level => 3, os => 'linux', pager => 'man-duty', }
      Wow! Thanks, that does the trick. I agree with you completely about the structure of the has, but I don't have control over that. Also thanks for give me a increase appreciation for the keys command.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699762]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found