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
17.4.3.1.1.0.37.17.87.107.181does not exist in both hashes. Maybe one of the keys has embedded spaces, newlines, or NULL bytes? Try stepping through with the debugger, or usingData::Dumperto inspect\%macAddrHashand\%bridgePortHash. - mob