in reply to Merging hashes at key match

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.

Replies are listed 'Best First'.
Re^2: Merging hashes at key match
by GeorgMN (Acolyte) on Mar 23, 2016 at 08:48 UTC
    Thank you all kindly for you input! Georg
Re^2: Merging hashes at key match
by GeorgMN (Acolyte) on Mar 23, 2016 at 14:19 UTC

    Content of the files i am trying to combine, or at least add the IP address to physical interface together:

    [gtreptow@nostromo show_outputs]$ cat *arp* | grep 10\.60 ARPA Vlan2010 Internet 10.60.198.59 1 d8d3.85a5.e3c4 ARPA Vlan198 Internet 10.60.226.31 0 0050.56b0.3d17 ARPA Vlan2260 Internet 10.60.227.25 1 0050.56b0.7e6e ARPA Vlan2270 Internet 10.60.226.24 0 0050.56b0.3580 ARPA Vlan2260 Internet 10.60.228.31 2 0050.56b0.3569 ARPA Vlan2280 Internet 10.60.226.25 0 0050.56b0.7fb0 ARPA Vlan2260 Internet 10.60.249.1 1 0009.0f09.0a01 ARPA Vlan2490 Internet 10.60.198.62 3 18a9.0559.c862 ARPA Vlan198 Internet 10.60.197.61 1 0015.1766.1cc8 ARPA Vlan1970 Internet 10.60.227.26 0 0050.56b0.7f8a ARPA Vlan2270 Internet 10.60.197.60 2 047d.7b4a.70ee ARPA Vlan1970 Internet 10.60.226.27 0 0050.56b0.19a3 ARPA Vlan2260 Internet 10.60.250.28 4 001b.90f0.67c1 ARPA Vlan250 Internet 10.60.249.31 0 d89d.67f4.495c ARPA Vlan2490 Internet 10.60.227.5 - 0008.e3ff.fd90 ARPA Vlan2270 [gtreptow@nostromo show_outputs]$ cat hcs51-4_sh_mac_add.txt HCS51-4# sh mac address-table | ex Po24 Legend: * - primary entry, G - Gateway MAC, (R) - Routed MAC, O - Over +lay MAC age - seconds since last seen,+ - primary entry using vPC Peer +-Link VLAN MAC Address Type age Secure NTFY Ports/SWI +D.SSID.LID ---------+-----------------+--------+---------+------+----+----------- +------- * 3920 000c.2999.7b8c dynamic 0 F F Eth101/1/33 * 3920 000c.29b5.f5b3 dynamic 0 F F Eth101/1/33 * 3920 0014.a998.c962 dynamic 0 F F Eth102/1/27 * 2491 0050.56b0.3858 dynamic 0 F F Eth101/1/5 * 2491 0050.56b0.5c3e dynamic 20 F F Eth101/1/5 * 2491 0050.56b0.608e dynamic 0 F F Eth101/1/5 * 2491 0050.56b0.66fc dynamic 0 F F Eth101/1/5 * 2491 0050.56b0.7d2b dynamic 0 F F Po21 * 2491 0050.56b0.7e40 dynamic 0 F F Eth101/1/5 * 2491 d89d.67f4.495c dynamic 0 F F Eth101/1/15 * 2280 0050.56b0.52e8 dynamic 0 F F Po21 * 2280 0050.56b0.57df dynamic 0 F F Eth101/1/14 * 2280 0050.56b0.7258 dynamic 0 F F Po21 * 2280 0050.56b0.7e80 dynamic 0 F F Po21 * 2280 0050.56b0.7f99 dynamic 0 F F Po21 * 2280 0050.56b0.7f9a dynamic 0 F F Po21 * 2280 0050.56b0.7fe5 dynamic 0 F F Po21 * 2271 0050.56b0.1186 dynamic 0 F F Eth101/1/5 * 2271 0050.56b0.1eee dynamic 10 F F Eth101/1/5 * 2271 0050.56b0.24f1 dynamic 0 F F Eth101/1/14 * 2271 0050.56b0.7e6e dynamic 0 F F Eth101/1/5 * 2271 0050.56b0.7f8a dynamic 0 F F Eth101/1/5 * 2261 0050.56b0.221d dynamic 0 F F Eth101/1/14 * 2261 0050.56b0.357f dynamic 0 F F Eth101/1/14 * 2261 0050.56b0.7e59 dynamic 0 F F Eth101/1/14 * 2261 0050.56b0.7f8e dynamic 0 F F Eth101/1/14 * 2260 0050.56b0.19a3 dynamic 0 F F Eth101/1/14 * 2260 0050.56b0.7de5 dynamic 0 F F Eth101/1/14 * 2240 0050.56b0.0010 dynamic 0 F F Eth101/1/14 * 2240 0050.56b0.0012 dynamic 0 F F Eth101/1/14 * 2240 0050.56b0.2556 dynamic 0 F F Eth101/1/14 * 2240 0050.56b0.6261 dynamic 0 F F Eth101/1/14 * 2040 0050.56b0.002f dynamic 100 F F Eth101/1/14 [gtreptow@nostromo show_outputs]$ ... and so on....
      How about this? (I tried to made it simple and readable, instead of terse. But ask if you do not understand a part, or do not know how to extend it to what you need)

      #!/usr/bin/perl use strict; use warnings; my @FILENAMES_ARP = <./*arp*.txt>; my @FILENAMES_MAC = <./*mac*.txt>; # use File::Slurp; this way this works: my @LINES =read_file($FILE + ); my %MACS; my $FILE; my $CLEAN_ARP_REC = './clean_arp.tmp'; open (OUTPUT, ">", $CLEAN_ARP_REC) or die "Could not open $CLEAN_ARP_R +EC because $!"; for $FILE (@FILENAMES_MAC){ print "processing $FILE...\n"; if( open(FH, "<", $FILE) ){ while(<FH>){ chomp; next unless /^\*/; # skip if line does not start with "*", + for example, empty lines next if /static/; # skip static entries my @F = split(/\s+/, $_); my $VLAN = $F[1]; my $MAC =$F[2]; my $INT = $F[7]; next unless $MAC=~/^[0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}$ +/; if($MACS{$MAC}){ warn "$MAC already defined, skipping entry at line $.\ +n"; }else{ $MACS{$MAC}{VLAN}=$VLAN; $MACS{$MAC}{INT}=$INT; } } close FH; }else{ warn "Could not open $FILE, skipping. Errormsg=$!\n"; } } for $FILE (@FILENAMES_ARP){ print "Processing $FILE...\n"; if( open(FH, "<", $FILE) ){ while(<FH>){ chomp; my @F = split(/\s+/, $_); my $MAC = $F[3]; my $VLAN = $F[5]; my $IP = $F[1]; next unless $IP; # must be defined? if($MACS{$MAC}){ $MACS{$MAC}{IP}=$IP; # add this extra data $MACS{$MAC}{_}=1; # have to "merge" } } close FH; }else{ warn "Could not open $FILE, skipping. Errormsg=$!\n"; } } for my $MAC (sort keys %MACS){ if( defined $MACS{$MAC}{_} ){ print OUTPUT $MAC . ", " . $MACS{$MAC}{IP} . ", " . $MACS{$MAC}{VLAN} . ", " . $MACS{$MAC}{INT} ."\n"; }else{ # normal line, do not print ? } } close OUTPUT;