Perl 触发器运算符 - 全局状态问题?

发布于 2025-01-07 06:53:49 字数 1626 浏览 4 评论 0原文

我正在使用触发器运算符进行一些文本解析,我的数据如下所示:

COMMAND START

CELL
123

COUNTER1    COUNTER2    COUNTER3
23          25          45

COUNTER1    COUNTER2    COUNTER3
22          34          52

CELL
234

COUNTER1    COUNTER2    COUNTER3
12          35          35

END

现在我需要迭代这些数据,并为每个单元格找到 COUNTER2 的总和。我们可以看到,每个单元格下可能有 1 个或多个 COUNTER 行。 我尝试使用如下所示的触发器运算符,但它不起作用。

my ($CELL_ID_COL1, $TEMP_COUNT);
my @line_contents;

while (<>) {
    chomp;
    if(/^COMMAND.*START$/ .. /^END$/) {

        if (my $e1 = /^CELL$/ ... (/^CELL$/ || /^END$/)) {
            if ($e1 == 2) {
                @line_contents = split(/\s+/, $_);  #Split the line read on whitespaces
                $CELL_ID_COL1 = $line_contents[0];
                print "$CELL_ID_COL1\n";
                $TEMP_COUNT = 0;
            }

            if (my $e2 = /^COUNTER1.*COUNTER3$/ ...(/^COUNTER1.*COUNTER3$/ || /^CELL$/ || /^END$/) ) {
                print "$_\n";
                if ($e2 ==2) { 
                    @line_contents = split(/\s+/, $_);  #Split the line read on whitespaces
                    $TEMP_COUNT += $line_contents[1];
                }
                if ($e2 =~ /E0$/) {
                    redo;
                }

            }
            if ($e1 =~ /E0$/) {
                print "FINAL COUNT is: $TEMP_COUNT\n";              
                redo;
            }

        }
    }
}

我认为这与 问题,但不太明白。请帮我。

预先非常感谢。

I am doing some text parsing using flip-flop operator and my data looks like below:

COMMAND START

CELL
123

COUNTER1    COUNTER2    COUNTER3
23          25          45

COUNTER1    COUNTER2    COUNTER3
22          34          52

CELL
234

COUNTER1    COUNTER2    COUNTER3
12          35          35

END

Now i need to iterate through this data and for each CELL, find the Sum of COUNTER2. Under each cell as we can see there might be 1 or more COUNTER rows.
I tried using flip-flop operator like below, but its not working.

my ($CELL_ID_COL1, $TEMP_COUNT);
my @line_contents;

while (<>) {
    chomp;
    if(/^COMMAND.*START$/ .. /^END$/) {

        if (my $e1 = /^CELL$/ ... (/^CELL$/ || /^END$/)) {
            if ($e1 == 2) {
                @line_contents = split(/\s+/, $_);  #Split the line read on whitespaces
                $CELL_ID_COL1 = $line_contents[0];
                print "$CELL_ID_COL1\n";
                $TEMP_COUNT = 0;
            }

            if (my $e2 = /^COUNTER1.*COUNTER3$/ ...(/^COUNTER1.*COUNTER3$/ || /^CELL$/ || /^END$/) ) {
                print "$_\n";
                if ($e2 ==2) { 
                    @line_contents = split(/\s+/, $_);  #Split the line read on whitespaces
                    $TEMP_COUNT += $line_contents[1];
                }
                if ($e2 =~ /E0$/) {
                    redo;
                }

            }
            if ($e1 =~ /E0$/) {
                print "FINAL COUNT is: $TEMP_COUNT\n";              
                redo;
            }

        }
    }
}

I think this got to do something with the global state of flip-flop operator discussed in this question, but could not understand much. Please help me.

Thanks a lot in advance.

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

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

发布评论

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

评论(1

与酒说心事 2025-01-14 06:53:49

我认为没有触发器更容易。有时您一开始认为某个功能是可行的方法,但随后您就会遇到问题。不要克制住坚持使用该功能的冲动,直到将其强加到整体中,而是考虑一种完全不同的方式来实现它”。

use Data::Dumper;
use List::Util qw(sum);

my %cells;
my $cell_id;
my $started;

while( <DATA> ) {
    $started++ if  /COMMAND START/;
    next unless $started;
    next if /\A\s*\z/;
    last if /END/;

    if( /CELL/ ) {
        chomp( $cell_id = <DATA> );
        $index = 0;
        }

    if( /COUNTER/ ) {
        my @counters = ( <DATA> =~ /([0-9]+)/g );
        $cells{$cell_id}[$index++] = \@counters;
        }
    }

my %sums;
foreach my $cell_id ( keys %cells ) {
    $sums{$cell_id} =  sum map { $_->[1] } @{$cells{$cell_id}}
    }

print Dumper( \%cells, \%sums );

您还可以修改它以累积总和。

但是,如果您仍然想使用触发器,您可以阅读 尊重触发器运算符的全局状态制作专属触发器运算符

I think it's easier without the flip flop. Sometimes you start off thinking a feature is the way to go, but then you run into problems. Instead of resisting the urge to stick with the feature until you force it into the round whole, consider a completely different way to do it"

use Data::Dumper;
use List::Util qw(sum);

my %cells;
my $cell_id;
my $started;

while( <DATA> ) {
    $started++ if  /COMMAND START/;
    next unless $started;
    next if /\A\s*\z/;
    last if /END/;

    if( /CELL/ ) {
        chomp( $cell_id = <DATA> );
        $index = 0;
        }

    if( /COUNTER/ ) {
        my @counters = ( <DATA> =~ /([0-9]+)/g );
        $cells{$cell_id}[$index++] = \@counters;
        }
    }

my %sums;
foreach my $cell_id ( keys %cells ) {
    $sums{$cell_id} =  sum map { $_->[1] } @{$cells{$cell_id}}
    }

print Dumper( \%cells, \%sums );

You can also modify this to accumulate the sums as you go.

However, if you still want to use the flip flop, you can read Respect the global state of the flip flop operator and Make exclusive flip-flop operators

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