Reputation: 205
I have a file that contains several columns separated with \t
, and I want to get the position (index) of some columns by using regular expressions to specify the name of header of columns.
Here is my code, but I can't search using regular expressions:
#!"C:\xampp\perl\bin\perl.exe"
my %dic;
my %index;
while(<>) {
my @array2 = split(/\t/, $_);
@index{@array2} = (0..$#array2);
my $column13= $index{"name13"};// Here I want to search using regex
my $column17= $index{"name17"};// Here I want to search using regex
my $column21= $index{"name21"};// Here I want to search using regex
my $column34= $index{"name32"};// Here I want to search using regex
my $column43= $index{"name43"};// Here I want to search using regex
print $array2[$column13]$.",".$array2[$column17].",".$array2[$column21].
",".$array2[$column34].",".$array2[$column43]."\n";
}
For example the value of $columns13
should be 12 (position 12) and:
$column17 = 16
$column21 = 20
$column34 = 33
$column43 = 42
My input is a file that contains several columns separated with \t
:
name1 name2 name3... name85
1 2 3 4 .... 765
6 5 9 67 .... 8768
87 787 767 7687 ...... 8768
My output should contains only the colums that have been searched:
name13 name17 name21... name43
876 76 87 4 .... 87687
787 987 9 67 ... 87686
53 765 767 7687 .... 8686
Upvotes: 1
Views: 98
Reputation: 126722
You're specification is rather sloppy, but I think this will do as you ask. It takes the first non-blank line from the input as the header line, and creates a list of corresponding indices in @indices
. The corresponding columns from each subsequent are printed to STDOUT.
use strict;
use warnings;
my @selection = qw(
name1
name3
name85
);
my @indices;
while (<>) {
next unless /\S/;
chomp;
my @fields = split /\t/;
unless (@indices) {
@indices = grep {
my $i = $_;
grep { $fields[$i] =~ /$_/ } @selection;
} 0 .. $#fields;
}
print join("\t", @fields[@indices]), "\n";
}
Upvotes: 1