1

请帮忙

我正在处理一个文件,其数据行如下所示。可以看出,数据被 ' |||' 分成了 4 个,所以我会有四个数组(如果我把它分开的话)。我想要的是这个:

  1. 我想检查第一个数组中是否有标点符号,如果有,请记住数组中的位置。
  2. 转到第三个数组中的相同位置,并读取括号中的数字。
  3. 检查数字的数组索引处的值是否是标点符号。

我的问题是我不记得比赛及其位置!你能帮忙吗?

útil por la unión europea , 一个 ||| 由欧盟,||| () (0) (1) (3) (2) (4) () ||| (1) (2) (4) (3) (5)
4

3 回答 3

6

除了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";
}   
于 2009-05-16T00:36:42.883 回答
5

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寻找它们)

于 2009-05-16T00:26:12.487 回答
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];
   }

类似的东西...

于 2009-05-16T00:41:33.683 回答