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

发布于 2024-07-20 16:00:48 字数 386 浏览 7 评论 0原文

请帮助

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

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

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

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

Please help

I am working with a file whose lines of data look like the one below. As can be seen, the data is divided into 4 by '|||', so I will have four arrays( if I divide it). what I want is this:

  1. I want to check if there are punctuation marks in the first array, if there is one, remember the position in the array.
  2. Go to the same position in the third array, and read the number in the bracket.
  3. Check if the value at the array index of the number is punctuation.

My problem is that I could not remember the match and its position! Can you help here, please?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素食主义者 2024-07-27 16:00:48

除了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";
}   

In addition to pos(), there are @- and @+:

#!/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";
}   
百合的盛世恋 2024-07-27 16:00:48

pos() 函数可用于报告比赛的(结束)位置。 示例:

my $string = 'abcdefghijk';

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

此代码将打印“There is an 'e'结尾于位置 5。” (位置从 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< /a>

The pos() function can be used to report the (ending) position of a match. Example:

my $string = 'abcdefghijk';

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

This code will print, "There is an 'e' ending at position 5." (Positions start from 0.) Combine this with the normal use of capturing parentheses and you should be able to solve your problem.

In addition to pos(), there are also the special global arrays @- and @+ which provide the start and end offsets of each subpattern matched. Example:

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";
}

( Thanks to Chas. Owens for jogging my memory on these; I was looking in perlre for them instead of in perlvar )

永言不败 2024-07-27 16:00:48

当您需要在代码中完成一些并不简单的事情时,最好将其分解为离散的步骤和变量,以便于理解。

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

#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];
   }

类似的东西......

When you have something to do something in code that isn't simple, it's best to break it down into discrete steps and variables so that it is easy to understand.

So I would first split the data string into it's four parts:

#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];
   }

Something like that ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文