Perl 触发器运算符 - 全局状态问题?
我正在使用触发器运算符进行一些文本解析,我的数据如下所示:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有触发器更容易。有时您一开始认为某个功能是可行的方法,但随后您就会遇到问题。不要克制住坚持使用该功能的冲动,直到将其强加到整体中,而是考虑一种完全不同的方式来实现它”。
您还可以修改它以累积总和。
但是,如果您仍然想使用触发器,您可以阅读 尊重触发器运算符的全局状态 和 制作专属触发器运算符
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"
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