如何在Perl中记住匹配及其在数组中的位置?

时间:2009-05-16 00:14:40

标签: perl position match

请帮忙

我正在使用一个文件,其数据行如下所示。可以看出,数据按'|||'分为4,所以我将有四个数组(如果我将它除以)。我想要的是这个:

  1. 我想检查第一个数组中是否有标点符号,如果有,则记住数组中的位置。
  2. 转到第三个数组中的相同位置,并读取括号中的数字。
  3. 检查数字的数组索引处的值是否为标点符号。
  4. 我的问题是,我记不起比赛了,它的位置!你能在这帮忙吗?

    útil por la unión europea , a ||| by the european union , ||| () (0) (1) (3) (2) (4) () ||| (1) (2) (4) (3) (5)
    

3 个答案:

答案 0 :(得分:5)

pos()外,还有@-@+

#!/usr/bin/perl

use strict;
use warnings;

my $string = "foo bar baz";

if ($string =~ /(foo) (bar) (baz)/) {
    print "the whole match is between $-[0] and $+[0]\n",
        "the first match is between $-[1] and $+[1]\n",
        "the second match is between $-[2] and $+[2]\n",
        "the third match is between $-[3] and $+[3]\n";
}   

答案 1 :(得分:4)

pos()功能可用于报告匹配的(结束)位置。例如:

my $string = 'abcdefghijk';

if($string =~ /e/g)
{
  print "There is an 'e' ending at position ", pos($string), ".\n";
}

此代码将打印,“位置5处有'e'结尾。” (位置从0开始。)将此与捕获括号的正常使用相结合,您应该能够解决问题。

pos()外,还有特殊的全局数组@-@+,它们提供匹配的每个子模式的起始和结束偏移。例如:

my $string = 'foo bar baz';

if($string =~ /(foo) (bar) (baz)/)
{
  print "The whole match is between $-[0] and $+[0].\n",
        "The first match is between $-[1] and $+[1].\n",
        "The second match is between $-[2] and $+[2].\n",
        "The third match is between $-[3] and $+[3].\n";
}

感谢Chas.Owens为我慢慢记忆;我在perlre寻找他们,而不是perlvar

答案 2 :(得分:1)

当你在代码中做一些不简单的事情时,最好把它分解成离散的步骤和变量,以便它易于理解。

所以我首先将数据字符串分成四个部分:

#The data record
my $dataRec = "útil por la unión europea , a ||| by the european union , ||| () (0) (1) (3) (2) (4) () ||| (1) (2) (4) (3) (5)";

#split it into four parts
my ($Native, $English, $data1, $data2) = split(/\|\|\|/,$dataRec);

#Store the position of the punctuation mark
my $puncPos = index($Native, ",");

#If we found the punctuation mark, parse the data
my @dataList;
my $dataValue;
if ( $puncPos != -1 )
   {
   @dataList = split(/[)( ]/,$data1);

   # use the punctuation position as the index into the array of values parsed
   $dataValue = $dataList[$puncPos];
   }

像这样......