简体   繁体   English

Perl:使用正则表达式搜索元素的索引

[英]Perl: search index of element using regex

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. 我有一个包含用\\t分隔的几列的文件,并且我想通过使用正则表达式指定列标题的名称来获取某些列的位置(索引)。 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: 例如, $columns13的值应为12(位置12),并且:

 $column17 = 16
 $column21 = 20
 $column34 = 33
 $column43 = 42

My input is a file that contains several columns separated with \\t : 我的输入是一个文件,其中包含用\\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

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 . 它以输入中的第一条非空白行作为标题行,并在@indices创建相应索引的列表。 The corresponding columns from each subsequent are printed to STDOUT. 随后的每个对应列将打印到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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM