Leser: 30
1
2
3
%temphash = ('1AHIT' => {'COMMENT', '1AHIT', 'IP', '192.168.221.10'}, '2AHIT' => {'COMMENT', '2AHIT', 'IP', '192.168.222.10'},
'CAD1-OST' => {'COMMENT', 'CAD1-OST' , 'IP', '192.168.43.10'}
....);
@keys_sorted_by_value = sort{$temphash{$a}{'IP'} <=> $temphash{$b}{'IP'}} keys %temphash;
@keys_sorted_by_text = sort{$temphash{$a}{'IP'} cmp $temphash{$b}{'IP'}} keys %temphash;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use strict; use warnings; my %temphash = ( '1AHIT' => { 'COMMENT' => '1AHIT', 'IP' => '192.168.221.10' }, '2AHIT' => { 'COMMENT' => '2AHIT', 'IP' => '192.168.222.10' }, 'CAD1-OST' => { 'COMMENT' => 'CAD1-OST', 'IP' => '192.168.43.10' } ); my @keys_sorted=sort{$temphash{$b}{'IP'} cmp $temphash{$a}{'IP'}} keys(%temphash); for my $key (@keys_sorted) { print "$key:\n"; print " IP: ".$temphash{$key}{IP}."\n"; print " COMMENT: ".$temphash{$key}{COMMENT}."\n"; print "#"x80,"\n"; }
1 2 3 4 5 6 7
my @keys = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [sprintf('%03d' x 4, split /\./, $temphash{$_}->{IP}), $_] } keys %temphash;
1 2 3 4 5 6 7 8
use Net::IP qw/ ip_iptobin /; my @keys = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ip_iptobin($temphash{$_}->{IP}), $_] } keys %temphash;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/perl use strict; use warnings; use Net::IP qw/ip_iptobin/; my %temphash = ( '1AHIT' => { COMMENT => '1AHIT', IP => '192.168.221.10' }, '2AHIT' => { COMMENT => '2AHIT', IP => '192.168.222.10' }, 'CAD1-OST' => { COMMENT => 'CAD1-OST', IP => '192.168.43.10' } ); my @keys_sorted_by_ip = sort { ip_iptobin($temphash{$a}{IP}, 4) cmp ip_iptobin($temphash{$b}{IP}, 4) } keys %temphash; print map { "$_: IP = $temphash{$_}{IP}\n" } @keys_sorted_by_ip;