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
print $array2[$column13].","....can be written much simpler with an array slice and join:print join(",", @array2[$column13, $column17, ...]), "\n";