Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Why not using an array?

by pklausner (Scribe)
on Jan 10, 2012 at 13:22 UTC ( [id://947163]=note: print w/replies, xml ) Need Help??


in reply to Re: Looping Over Hash Skips an Element?
in thread Looping Over Hash Skips an Element?

From them names I guesstimate he populates this table from the SNMP interface MIB. The index of the ifTable is not necessarily contiguous. Some devices give you 1,2,3.. then jump to 100 or even 1000. So that is not practical as array index. I had the same problem and solved it with hashes plus a custom sort function:
sub sort_by_intf # sort interface alpha/number name mixture properly { my $A = $a; # just turn any number into a 0004 digit one my $B = $b; $A =~ s/([0-9]+)/ sprintf "%04d",9999-$1 /ge; $B =~ s/([0-9]+)/ sprintf "%04d",9999-$1 /ge; $B cmp $A; # reverse order happens to be useful here: } # Tun0, Ser1, Port-channel2, Gi3, Fa4, Eth5
Then whenever you loop over the keys, just sort them with that function like so:
for (sort sort_by_intf keys %ifSpeed) { printf "intf: %s, speed: %d\n" $_, $ifSpeed{$_}; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found