@ARGV or die "No input file specified"; open $first, '<',$ARGV[0] or die "Unable to open input file: $!"; open $second,'<', $ARGV[1] or die "Unable to open input file: $!"; print scalar (<$first>); while(<$second>){ chomp; @line=split /\s+/; $hash{$line[1]}{$line[2]}{$line[3]}= $line[0]; } while (<$first>) { @cols = split /\s+/; foreach $key1 (sort keys %hash) { foreach $key2 (sort keys %{$hash{$key1}}) { foreach $key3 (sort keys %{$hash{$key1}{$key2}}) { if (($cols[2] eq $key1) && ($cols[4]>=$key2) && ($cols[5]<=$key3)){ print join("\t",@cols),"\t",$hash{$key1}{$key2}{$key3},"\n"; } last; } } } }