in reply to Re^2: Merging hashes at key match
in thread Merging hashes at key match

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;