use Data::Dumper; use strict; use warnings; my $hash; open(FILE, "<", "file1.txt"); while () { my ($k, $v) = split " "; $hash->{$k}->[0] = $v; } close FILE; open(FILE, "<", "file2.txt"); while () { my ($v, $k) = split " "; $k = /(complex\.\d+)/; $hash->{$1}->[1] = $v; } close FILE; print Dumper($hash); open (OUT, ">", "out.txt"); for my $key (keys(%$hash)) { print OUT $key . " " . $hash->{$key}->[0] . " " . $hash->{$key}->[1] . "\n"; } close OUT;