Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Sorting hash

by bartrad (Beadle)
on Apr 05, 2018 at 17:58 UTC ( [id://1212362]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, after a bit of advice if I could please? I have the following hash that I'm trying to sort by the value against CPU.

$VAR1 = { 'hostname' => { '1' => { 'pass' => + 1, 'cpu' => +'0.07%', 'box_name +' => 'hostname', 'capacity +' => '0.41%' } }, 'hostname' => { '1' => { 'pass' => 1 +, 'cpu' => '0 +.04%', 'box_name' +=> 'hostname', 'capacity' +=> '0.25%' } },

Here's my code, but I'm getting

Use of uninitialized value in numeric comparison (<=>) at ./cpu_check.pl line 157.

so I must not be doing it properly?

foreach my $router ( sort {$results->{1}{cpu}{$a} <=> $results->{1}{cpu}{$b} } keys %$results )

Thank you

Replies are listed 'Best First'.
Re: Sorting hash
by ablanke (Monsignor) on Apr 05, 2018 at 18:37 UTC

    Hi,

    since your Dump contains 2 'hostname' elements, i think you really have a ArrayRef which contains HashRefs.

    you also have to take care of your sorting.

    hopefully my example code helps:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; #Hash Number 1 my %hash1 = ( 'hostname' => { '1' => { 'pass' => 1, 'cpu' => '0.07%', 'box_name' => 'hostname', 'capacity' => '0.41%' } } ); #Hash Number 2 my %hash2 = ( 'hostname' => { '1' => { 'pass' => 1, 'cpu' => '0.04%', 'box_name' => 'hostname', 'capacity' => '0.25%' } } ); #Array of Hash Refs my @array = ( \%hash1, \%hash2 ); #Array Ref my $results = \@array; #Result Elements (Hash Refs $a and $b) are dereferenced and values of +key cpu is used for sorting + foreach my $router ( sort { $a->{'hostname'}{1}{cpu} <=> $b->{'hostnam +e'}{1}{cpu} } @{$results} ) { print Dumper($router); }

      Hi, thanks for the reply. Sorry, I redacted the data where hostname sat. Those values are in fact unique for each key.

      'hostname1' => { '1' => { 'pass' => 1, 'cpu' => '0.1 +1%', 'box_name' => + '', 'capacity' => + '0.57%' } }, 'hostname2' => { '1' => { 'pass' => +1, 'cpu' => ' +0.09%', 'box_name' + => '', 'capacity' + => '0.43%' } },

        Hi,

        there you go.

        i must confess that i missed also 1 thing in your sort function.

        the cpu percentage will not interpreted as an numeric value, so you have to use cmp instead of <=>.

        see perlop

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %results = ( 'hostname1' => { '1' => { 'pass' => 1, 'cpu' => '0.07%', 'box_name' => 'hostname', 'capacity' => '0.41%' } }, 'hostname2' => { '1' => { 'pass' => 1, 'cpu' => '0.04%', 'box_name' => 'hostname', 'capacity' => '0.25%' } } ); my $results = \%results; foreach my $router ( sort { $results->{$a}{1}{cpu} cmp $results->{$b}{ +1}{cpu} } keys %{results} ) { print Dumper($results->{$router}); }
Re: Sorting hash
by pryrt (Abbot) on Apr 05, 2018 at 18:50 UTC

    Your example shows you indexing the hash keys in the wrong order; it was trying to find the key named "1" in the %$results hash, which didn't exist. Use: foreach my $router ( sort {$results->{$a}{1}{cpu} <=> $results->{$b}{1}{cpu} } keys %$results ).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found