my $col = ""; foreach my $key (keys %sampleValue){ my @col1 = split("\t",$sampleValue{$key}); $col = $col1[1].",".$col; } chop($col); print $col,"\n"; ## string of all the columns I am interested in #### my @cols = sort values %sampleValue; #### ## Reading the sample Value file while (<$IN>) { chomp; print $_, "\n"; my @line = split "\t"; @line = @line[@cols]; # <-- Use array slice here ...