#!/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;