my %hash1; open my $IN, "file1.txt" or die $!; my $header = <$IN>; ## use if the file has a header otherwise leave it while(<$IN>){ chomp $_; my @line = split(',',$_); ## split the line to get the sample name and its values $hash1{$line[0]} = 1; ## if your first column is a sample name or change it accordingly } close($IN); #### my %CommonSampleNames open my $IN1, "file2.txt" or die $!; my $head = <$IN1> ;## again use it if it has a header while(<$IN1>){ chomp $_; my @line = split(',', $_); if(exists $hash1{$line[0]} ){ ## $line[0] is the sample name column $CommonSampleNames{$line[0]} = 1; } } close($IN1); #### my %supHash open my $ref, "ref.txt" or die $!; $head = <$ref>; ## if header is present while(<$ref>){ chomp $_; my @line = split(',', $_); ## if it is a csv file if(exists $CommonSampleNames{$line[0]} ){ $supHash{$ine[0]} = $line[1]; ## if second column has reference names } } close($ref);