in reply to Extracting Columns from line

You can use map and grep to good effect here:

use strict; use warnings; open my $IN, "sample.txt" or die $!; my $header = <$IN>; my %sampleID = map { /(.*?)\t/; $1 => 1 } <$IN>; # store desired colum +ns close($IN); open $IN, "sampleValue.txt" or die $!; $header = <$IN>; my @samples = split /\t/, $header; my @cols = grep { exists $sampleID{$samples[$_]} } 0..$#samples; # sto +re indices of desired columns while(<$IN>){ chomp; my @line = (split /\t/)[@cols]; # pick desired columns using array + slice print join( "\t", @line ), "\n"; }