1

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
11
  • 1
    Perhaps you should give an example of what you are trying to do, with input and expected output. Commented Dec 25, 2014 at 15:38
  • Ok I will add example in my question thanks Commented Dec 25, 2014 at 15:39
  • 1
    Your print statement print $array2[$column13].",".... can be written much simpler with an array slice and join: print join(",", @array2[$column13, $column17, ...]), "\n"; Commented Dec 25, 2014 at 15:41
  • ok Thanks. I added the input and output in my question Commented Dec 25, 2014 at 15:48
  • I want only search specified name using regex in headr of columns if match then it should give my the position of this column Commented Dec 25, 2014 at 15:50

1 Answer 1

1

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";
}
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks for your response, but I don't have an array that contains the header, the header is the first line in the file who I give as argument
And what is @selection please?
I can't help you if you won't answer where your list of wanted column headers comes from.
I have an input file, the first line is considered as headers, after headers comes others lines that contains values, and I execute my code it works i have any other inputs, I want to search the position of columns by specifyin the name of a header
Okay, so if you have no other inputs then there is no way to specify the names of the headers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.