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$/

In reply to Re: Merging 2 hashes by jonadab
in thread Merging 2 hashes - updated again by rspence

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.