Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Sorting a hash in a predetermined order

by edimusrex (Monk)
on Nov 17, 2015 at 22:46 UTC ( #1147959=perlquestion: print w/replies, xml ) Need Help??

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

I am once again seeking some help (it's been a while which is good...I think?). What I am trying to do it sort a hash in a predetermined order by its value. I am sure there is an easy way to accomplish this but I'm a little stuck on it.

Here's the code thus far :

#!/usr/bin/perl use strict; use warnings; use Net::Telnet::Cisco; my $host = 'voip-cme.compsciresources.com'; my $ssh = Net::Telnet::Cisco->new(Host => $host); $ssh->login('xxxx','xxxx'); my %int; my @output = $ssh->cmd("show ip interface brief"); foreach my $item(@output){ chomp $item; $item =~ s/^\s+//; my($interface,$ip,$ok,$method,$status,$protocol) = split(/\s+/,$it +em); next if !defined $interface or $interface eq 'Interface'; next if $ip eq 'unassigned'; $int{$interface}{ip} = $ip; $int{$interface}{ok} = $ok; $int{$interface}{method} = $method; $int{$interface}{status} = $status; $int{$interface}{protocol} = $protocol; } foreach my $member (keys %int){ print "$member: "; foreach my $mem_value (keys %{$int{$member}}){ print "$mem_value = $int{$member}{$mem_value} "; } print "\n"; }

I would like to order the output in the order in which I fill the hash as listed above, so (ip, ok, method, status, protocol). This is going to eventually be used as a nagios check and it would be great if the output could be in a consistent order.

Thank you for your wisdom

Replies are listed 'Best First'.
Re: Sorting a hash in a predetermined order
by choroba (Archbishop) on Nov 17, 2015 at 23:01 UTC
    If the keys are hardcoded, there's no need to retrieve them from the hash:
    for my $mem_value (qw( ip ok method status protocol )) {
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Sorting a hash in a predetermined order
by johngg (Canon) on Nov 17, 2015 at 23:13 UTC

    Store the order you want in an array.

    $ perl -Mstrict -Mwarnings -E ' my %people; $people{ Fred }->{ age } = 37; $people{ Fred }->{ sex } = q{male}; $people{ Fred }->{ occ } = q{plumber}; $people{ Mary }->{ age } = 41; $people{ Mary }->{ sex } = q{female}; $people{ Mary }->{ occ } = q{barmaid}; my @order = qw{ age sex occ }; foreach my $person ( keys %people ) { say qq{$person:}; foreach my $attr ( @order ) { say qq{ $attr: $people{ $person }->{ $attr }}; } }' Mary: age: 41 sex: female occ: barmaid Fred: age: 37 sex: male occ: plumber $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Sorting a hash in a predetermined order
by dasgar (Priest) on Nov 17, 2015 at 23:33 UTC
    I would like to order the output in the order in which I fill the hash as listed above...

    That kind of sounds like you're wanting an ordered hash. My first thought on that was Tie::IxHash. In looking for the link for that module, I stumbled across another module that does something similar and it had a link to yet another module (Hash::Ordered). The documentation for Hash::Ordered has links to other similar modules as well as some benchmarks run by its author comparing the different modules that provide an ordered hash.

    Of course, based on your full description, I'm thinking that choroba's suggestion would be what I probably would go with.

Re: Sorting a hash in a predetermined order
by BillKSmith (Prior) on Nov 18, 2015 at 04:55 UTC
    Strictly speaking, you cannot "sort a hash". However, you can control the order in which elements are processed (or printed). See the FAQ (perldoc -q "How do I sort a hash")
    Bill
Re: Sorting a hash in a predetermined order
by ctilmes (Vicar) on Nov 18, 2015 at 01:48 UTC

    I agree with others, hard code the list.

    This is going to eventually be used as a nagios check and it would be great if the output could be in a consistent order.

    BTW -- if all you want is a consistent order, that will be true. As long as you have the same set of hash keys, they will output in the same consistent order. (at least for the same version of perl..)

    If you want them in a specific order, that's when you need one of the other solutions.

      Hello ctilmes,

      As long as you have the same set of hash keys, they will output in the same consistent order. (at least for the same version of perl..)

      Not since v5.18.0! From perl5180delta:

      By default, two distinct hash variables with identical keys and values may now provide their contents in a different order where it was previously identical.
      ...
      The seed used by Perl's hash function is now random. This means that the order which keys/values will be returned from functions like keys(), values(), and each() will differ from run to run.

      Of course, the order will remain consistent for a given, unchanged, hash variable throughout a single run of the program. But that won’t satisfy the OP’s requirements.

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Sorting a hash in a predetermined order
by edimusrex (Monk) on Nov 18, 2015 at 15:28 UTC
    Cool, I was thinking hard coding was the solution. I will probably go down that path as it seems to be much more reliable. Thanks for all your input
Re: Sorting a hash in a predetermined order
by edimusrex (Monk) on Nov 18, 2015 at 15:33 UTC
    With the hard coded logic I ended up doing the following

    foreach my $member (keys %int){ print "$member: \n\tIP - $int{$member}{ip}\n\tOK? - $int{$member} +{ok}\n\tMethod - $int{$member}{method}\n\tStatus - $int{$member}{stat +us}\n\tProtocol - $int{$member}{protocol}\n\n"; }


    It does exactly what I want it to. Thanks again

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-09-24 16:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?