EDITOR's NOTE: the following node has been updated from it's original wording. You can view the orginal question here: http://perlmonks.thepen.com/303087_orig.html

Good Evening Reveared monks,

My problem was not in my nested loop - although as clunky (senseless, inefficient you say) I still think it would work - but before I even get to it. As I suspected since I close both while loops that create the 2 hashes *before* I get to the loop perl gives it only the end of each hash to play with. I don't know this for a fact, but, upon leaving the first while open then opening the second fh and second while loop, thus having "everything in memory" the loop works. (I've left the commented lines leaving each while loop open) I have included below the first iteration of the script that works using sauoq's latest loop suggestion, with a slight edit. Now on to the final snmp hash and merging it with this one.

My apologies for misusing any monk's cycles. I asked for a solution to a problem that was misdiagnosed. Alas the socratic method....

#!/usr/bin/perl -w # # # mac-bp-ifIndex.pl - a test script to gather the 1.3.6.1.2.1.17.4.3.1 +.2 OID # and the 1.3.6.1.2.1.17.1.4.1.2 OID information f +rom switches # (via snmp polls) then merge the tuples to create + a single # MAC -> ifIndex pairs list. # # # + use strict; + my $depot_dir="/nms/depot"; my $comm_string="xxx"; my $switch_list="/nms/switch_fetch/switch_list"; my $switch; my $mac; my $bp; my $bp2; my $ifIndex; my $snmpwalk; + my (%mactobp, %bptoifIndex); + open (SWITCH, "$switch_list") || die "Can't open $switch_list file"; open (LOG, ">$depot_dir/mac-bp-ifIndex.txt") || die "Can't open $depot +_dir/mac-bp-ifIndex.txt file"; + while (<SWITCH>) { chomp($switch="$_"); $snmpwalk="/usr/local/bin/snmpwalk -c $comm_string $switch "; + open(MACtoBP, "$snmpwalk .1.3.6.1.2.1.17.4.3.1.2 |") || die "c +an't do .1.3.6.1.2.1.17.4.3.1.2: $!\n"; + while (<MACtoBP>) { /SNMPv2-SMI::mib-2.17.4.3.1.2.(\d+)\.(\d+)\.(\d+)\.(\d ++)\.(\d+)\.(\d+)\s+\=\s+\w+:\s(\d+)$/gm; + $mac=sprintf ("%lx:%lx:%lx:%lx:%lx:%lx",$1, $2, $3, $4 +, $5, $6); $bp=$7; + %mactobp = ( $mac => "$bp" ); + # } + + open(BPtoifIndex, "$snmpwalk .1.3.6.1.2.1.17.1.4.1.2 |") || di +e "can't do .1.3.6.1.2.1.17.1.4.1.2: $!\n"; + while (<BPtoifIndex>) { /SNMPv2-SMI::mib-2.17.1.4.1.2.(\d+)\s+\=\s+\w+:\s(\d+) +$/gm; + $bp2=sprintf ("%d",$1); $ifIndex=sprintf ("%d",$2); + %bptoifIndex = ( $bp2 => "$ifIndex" ); + # } + + for my $mac (keys %mactobp) { if (exists $bptoifIndex{$bp} ) { $mactobp{$mac} = $bptoifIndex{$ifIndex}; print "$mac -> $ifIndex\n"; } } } } + } + close MACtoBP; close BPtoifIndex; close LOG; close SWITCH;

rspence

edited: Mon Nov 3 13:48:02 2003 by jeffa - added editor's note, readmore, formatting


In reply to 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.