Reputation: 39
I've got two hashes, with identical keys and different values. Specifically, they're the results of two different SNMP queries to recover the MAC addresses and Bridge Port IDs on a switch. The keys for both are identical, and are the SNMP string returned along with the value for each line of the query.
I.e. the MAC address hash will have a key/value pair of the following:
17.4.3.1.1.0.37.17.87.107.181/00 25 11 57 6B B5
Whereas the bridge port hash will have a corresponding key/value pair:
17.4.3.1.1.0.37.17.87.107.181/56
This is true for all entries in the hashes.
I thought it would be a simple matter of pulling the values for the snmp string on the left from each hash and putting them into a new one. However, the code below:
foreach $curSnmpId (@macKeys){
#Keys for macAddrHash and bridgePortHash are identical,
#so code below should pull corresponding entries out of
#each and put into macBridgeHash
my $curMacAddr = $macAddrHash{$curSnmpId};
my $curBridgeId = $bridgePortHash{$curSnmpId};
print "curSnmpId: $curSnmpId curMacAddr: $curMacAddr curBridgeId: $curBridgeId\n";
$macBridgeHash{$curBridgeId} = $curMacAddr;
}
Gives the following output:
curSnmpId: 17.4.3.1.1.0.37.17.87.107.181 curMacAddr: 00 25 11 57 6B B5 curBridgeId: curSnmpId: 17.4.3.1.1.0.0.116.250.193.119 curMacAddr: 00 00 74 FA C1 77 curBridgeId: curSnmpId: 17.4.3.1.1.0.35.24.202.193.125 curMacAddr: 00 23 18 CA C1 7D curBridgeId:
I've already checked that the bridge port hash has got data in it. I've also noticed that when I use the key set from the bridge port hash in place of @macKeys, I get the curBridgeId, but no curMacAddr.
Given that the value of $curSnmpId (e.g. 17.4.3.1.1.0.37.17.87.107.181) exists as a key in both hashes, why isn't my code working? I've been googling, searching and banging my head against this for the last day or so and would really appreciate any help.
Regards, tbdanny
Upvotes: 2
Views: 754
Reputation: 107040
First of all, are you using use strict;
and use warnings;
pragmas in your code? That will help detect syntactic errors that you might not normally see.
You should also do a bit of error checking too:
foreach my $curSnmpId (sort keys %macAddrHash) {
#
# This will warn you if there's no matching key in %bridgePortHash
#
if (not exists $bridgePortHash{$curSnmpId} {
warn "WARNING: Nonexistant Bridge Port Data at $curSnmpId";
}
print "curSnmpId: $curSnmpId "
print "curMacAddr: $macAddrHash{$curSnmpId} ";
#
# Due to `use warnings`, you'll get an error message if this is undefined
#
print "curBridgeId: $bridgePortHash{$curSnmpId}\n";
}
This way, you'll see the WARNING message if there the Bridge Port hash doesn't have the same key as in %macAddrHash. You'll get a warning, if the key entry exists, but the value is undefined when it tries to print out the value.
Another thing I'd suggest is to use Data::Dumper just to print out the two hashes and verify that they do have the same set of keys:
use Data::Dumper;
[...]
print Dumper ( \%macAddrHash ) . "\n";
print Dumper ( \%bridgePortHash ) . "\n";
This will print out the entire structure of both hashes. You'll have to go through it, but it'll help you see where your error in your data structure might be..
Another neat trick to verify something like this is to use Test::More to verify your keys are the same in both hashes:
use Test::More tests => 1;
[...]
is_deeply (\@{[sort keys %macAddrHash]}, \@{[sort keys %bridgePortHash]});
I have a feeling that you'll find that your data might not be what you think it is.
Upvotes: 3