my %genotype; while() { $genotype{$1} = $2 if (/(\d+)\t\w\t(\w)/); } while () { next unless (/(\d+)\t(\w)/); print OUT (defined($genotype{$1}) and ($genotype ne '')) ? "$1\t$genotype{$1}\n" : "$1\t$2\n"; }