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

Hello, I`ve wrote this piece of code that should return me a set of information via SNMP using get_table(). This is my code:
my $vresult = $session->get_table(-baseoid => $hardware_info); my %hashes = %{$vresult}; foreach my $key (keys %hashes){ print ($key," : ",$hashes{$key},"\n"); }
The problem is that when I run it it shows smth like this:
.1.3.6.1.4.1.26543.2.5.1.3.1.19.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.20.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.2.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.13.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.16.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.21.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.22.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.1.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.12.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.23.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.17.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.18.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.14.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.24.0 : .1.3.6.1.4.1.26543.2.5.1.3.1.15.0 :
I want those to be sorted smth like foreach my $key (sort keys %hashes){ but then it will show me :
.1.3.6.1.4.1.26543.2.5.1.3.1.1.0 .1.3.6.1.4.1.26543.2.5.1.3.1.12.0 .1.3.6.1.4.1.26543.2.5.1.3.1.13.0 .1.3.6.1.4.1.26543.2.5.1.3.1.14.0 .... .1.3.6.1.4.1.26543.2.5.1.3.1.2.0 .1.3.6.1.4.1.26543.2.5.1.3.1.20.0 .1.3.6.1.4.1.26543.2.5.1.3.1.21.0
How can I do to show me smth like this:
.1.3.6.1.4.1.26543.2.5.1.3.1.1.0 .1.3.6.1.4.1.26543.2.5.1.3.1.2.0 .1.3.6.1.4.1.26543.2.5.1.3.1.12.0 .1.3.6.1.4.1.26543.2.5.1.3.1.13.0 .. .1.3.6.1.4.1.26543.2.5.1.3.1.19.0 .1.3.6.1.4.1.26543.2.5.1.3.1.20.0 .1.3.6.1.4.1.26543.2.5.1.3.1.21.0
Hope you understand what I say..thanks.

Replies are listed 'Best First'.
Re: Net::SNMP question
by Corion (Patriarch) on Oct 23, 2008 at 13:29 UTC

    You can either pack the OIDs back into one large number and then sort on that number, or you can do a natural sort on the strings, either with one of the solutions posted by tye or by using Sort::Natural. A natural sort usually splits a string on numbers and then sorts the list by comparing non-numbers as strings and numbers as numbers.

      Thanks for your answers..Sort::Key::Natural helped me.
Re: Net::SNMP question
by Anonymous Monk on Oct 23, 2008 at 13:32 UTC
    pad everything :)
    #!/usr/bin/perl -- use strict; use warnings; my @un = qw[ .1.3.6.1.4.1.26543.2.5.1.3.1.19.0 .1.3.6.1.4.1.26543.2.5.1.3.1.20.0 .1.3.6.1.4.1.26543.2.5.1.3.1.2.0 .1.3.6.1.4.1.26543.2.5.1.3.1.13.0 .1.3.6.1.4.1.26543.2.5.1.3.1.16.0 .1.3.6.1.4.1.26543.2.5.1.3.1.21.0 .1.3.6.1.4.1.26543.2.5.1.3.1.22.0 .1.3.6.1.4.1.26543.2.5.1.3.1.1.0 .1.3.6.1.4.1.26543.2.5.1.3.1.12.0 .1.3.6.1.4.1.26543.2.5.1.3.1.23.0 .1.3.6.1.4.1.26543.2.5.1.3.1.17.0 .1.3.6.1.4.1.26543.2.5.1.3.1.18.0 .1.3.6.1.4.1.26543.2.5.1.3.1.14.0 .1.3.6.1.4.1.26543.2.5.1.3.1.24.0 .1.3.6.1.4.1.26543.2.5.1.3.1.15.0 ]; print $_,$/ for map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { my $f = $_; $f=~ s/(\d+)/sprintf '%06d', $1/ge; [ $_, $f ]; } @un;#keys %hashes __END__ .1.3.6.1.4.1.26543.2.5.1.3.1.1.0 .1.3.6.1.4.1.26543.2.5.1.3.1.2.0 .1.3.6.1.4.1.26543.2.5.1.3.1.12.0 .1.3.6.1.4.1.26543.2.5.1.3.1.13.0 .1.3.6.1.4.1.26543.2.5.1.3.1.14.0 .1.3.6.1.4.1.26543.2.5.1.3.1.15.0 .1.3.6.1.4.1.26543.2.5.1.3.1.16.0 .1.3.6.1.4.1.26543.2.5.1.3.1.17.0 .1.3.6.1.4.1.26543.2.5.1.3.1.18.0 .1.3.6.1.4.1.26543.2.5.1.3.1.19.0 .1.3.6.1.4.1.26543.2.5.1.3.1.20.0 .1.3.6.1.4.1.26543.2.5.1.3.1.21.0 .1.3.6.1.4.1.26543.2.5.1.3.1.22.0 .1.3.6.1.4.1.26543.2.5.1.3.1.23.0 .1.3.6.1.4.1.26543.2.5.1.3.1.24.0
    Schwartzian Transform