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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |