my @VLANS, @MACS, @INTS); push $HASH{$MACS[$mac]}, [$VLANS[$vlan], [$INTS[$int]];

Here, I assume, you want to store all found VLANS, and MAC addresses, however, instead of pushing them as blobs do this:

In each iteration you get a new $mac, just add one to the hash value of $mac $MACS{$mac}++;

This way, you can get a list back later, like so: @ALL_MACS = sort keys %MACS

AND, you can check if that MAC address came by more than once by checking the value $MACS{$mac}

But instead of that, one trick I often do is or-ing: like so:

When found in mac.txt $MACS{$mac} |= 1;

When found in arp.txt $MACS{$mac} |= 2;

This way, when $MACS{$mac} == 3 it is in both. And it can be multiple times in either one of them, the value is still 3.

Storing your data.

# make the data a string $value = join("|", $VLAN, $MAC, $INT); # put them into a 1 dimentional hash (however, servers with the same m +ac hash overwrite each other, although you can check) if( defined $HASH{$mac} ) { warn "Two servers with the same $mac"; }else{ $HASH{$mac}=$value; }

This way, you can iterate later over your $mac with:

for my $mac (keys %HASH){ my $value = $HASH{$mac}; ($VLAN, $MAC, $INT) = split("|", $value); }

# or put them in a multi-dimensional hash. $HASH{$mac}{$value}++;

This way, you can iterate later over your $mac with:

for my $mac (keys %HASH){ for my $value (keys %{$HASH{$mac}}){ } }

Now post some example mac.txt and arp.txt so we can really help you.


In reply to Re: Merging hashes at key match by FreeBeerReekingMonk
in thread Merging hashes at key match by GeorgMN

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.