in reply to Merging 2 hashes - updated again
Besides the problems with each that others have mentioned, you're doing something very inefficient here:
while (($bp2, $ifIndex) = each (%bptoifIndex)) { print "$bp2\n"; # testing tool if ($bp eq "$bp2") { $mactobp{$mac} = $ifIndex; print "$mac matches with $ifIndex.\n"; }
All this does is check whether there's a key in %bptoifIndex that matches $bp (and if so assign the associated value to $mactobp{$mac}), but it does this by looping through all the keys in the whole hash and testing each. However, a large part of the benefit of using hashes is that hash lookups are quick and easy, so you don't have to do this sort of schenaneghans. You can rewrite the above while loop as a simple conditional, like this:
if (exists $bptoifIndex{$bp}) { $mactobp{$mac} = $bptoifIndex{$bp}; print "$mac matches with $bptoifIndex{$bp}.\n" if $debug; } else { warn "$mac / $bp does not have an entry in %bptoifIndex\n"; }
This is not only faster, it's also a whole lot easier to read, IMO, and therefore will be easier for you to maintain down the road. Also, for the print statements that are just there for testing purposes, I usually like to suffix them all with if $debug so that when I'm done debugging I can turn them all off by commenting out one line at the top of the file ($debug=1;).
update: On second thought, you might be able to completely do away with this whole chunk of code, just by changing subsequent code from $mactobp{$mac} to $bptoifIndex{$mactobp{$mac}}, especially if you're pretty sure every mac has an ifIndex in the second hash, or if it's acceptable to get undef for ones that don't.
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
|
|---|