use strict; use warnings; ## Variables my %sampleID; my %sampleValue; ## Opening first file open my $IN, "sample.txt" or die $!; my $header = <$IN>; while(<$IN>){ chomp $_; my @line = split('\t', $_); $sampleID{$line[0]} = 1; ## Sample ID and Pam50 prediction } close($IN); print "Total number of sample ID: ", scalar(keys %sampleID),"\n"; ## 1000 columns ## Sample Value Data open $IN, "sampleValue.txt" or die $!; ## Columns are sample names from file1 $header = <$IN>; my @samples = split("\t", $header); ## print "Total samples: ",scalar(@samples),"\n"; ## 1500 ## loop for all the samples ids or the columns I am interested in for(my $i = 1; $i <= $#samples; $i++){ ## bcos the first instance is called header of the column 1 my $sample = $samples[$i]; $sampleValue{$sample} = $i if (exists $sampleID{$sample}); } my $col = ""; foreach my $key (keys %sampleValue){ $col = $sampleValue{$key}.",".$col; } chop($col); print $col,"\n"; ## string of all the columns I am interested in ## The reason I do the above loop because I don't want to look for the interested ## cols thru the hash for every line of the file ## Reading the sample Value file while(<$IN>){ chomp $_; print $_,"\n"; my @line = split("\t", $_); @line = @line[ split /,/, $col]; ## previously it was @line = @line[$col] -- and i was getting error because $col is a string print @line,"\n"; }