#!/usr/bin/perl -w use strict; my %file1hash; my ($fin1, $fin2) = qw|alpha.dat beta.dat|; open (DAT1,"$fin1"); while(my $line=) { chomp($line); next if $line=~m/^id/; my ($id,$x1,$x2)=split(' ' ,$line); push @{$file1hash{$id}}, "$x1 $x2"; } close DAT1; open (DAT2,"$fin2"); while (my $line=) { chomp($line); next if $line=~m/^id/; my ($id,$word)=split(' ', $line); if (exists $file1hash{$id}) { for my $x (@{$file1hash{$id}}){ print qq|$id\t$x\t$word\n|; } } } close DAT2; #### A 12 45 hello A 10 12 hello A 15 74 hello B 23 15 goodbye D 19 10 happy D 11 12 happy E 14 5 black E 34 31 black C 11 41 blue C 18 12 blue