触发器运算符/读取行计数器问题

发布于 2024-09-30 19:05:23 字数 302 浏览 3 评论 0原文

阅读Perl:从 1 到 n 提取行(Windows) 我不明白触发器运算符/读取行计数器部分。

perl -nE 'say $c if $c=1..3' my_file 

1
2
3E0

有人可以更详细地解释我这个输出来自哪里吗?

Reading this Perl: extract rows from 1 to n (Windows) I didn't understand the flip-flop-operator/readline-counter part.

perl -nE 'say $c if $c=1..3' my_file 

1
2
3E0

Could someone explain me more detailed where this output comes from?

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

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

发布评论

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

评论(1

戴着白色围巾的女孩 2024-10-07 19:05:23

引用perlop

在标量上下文中,“..”返回一个
布尔值。运营商是
双稳态,就像触发器一样,并且
模拟行范围(逗号)
sedawk 和各种运算符
编辑们。每个“..”运算符维护
它自己的布尔状态,甚至跨越
调用包含以下内容的子例程
它。只要是左边就是假的
操作数为假。一旦离开
操作数为 true,范围运算符
保持 true 直到正确的操作数出现
true,之后是范围运算符
又变成假的了。它不会成为
false 直到下一次范围
运算符被评估。它可以测试
正确的操作数并变为假
相同的评价成为事实(如
awk),但它仍然返回 true 一次。
如果你不想让它测试正确的
操作数直到下一次评估,如
sed 中,只需使用三个点(“...”)
而不是两个。在所有其他方面,
...”的行为就像“..”一样。

未计算正确的操作数
而操作符处于“假”状态
状态,并且左操作数不是
当操作员处于
“真实”状态。优先级是
略低于 ||&&。价值
返回的是空字符串
为 false,或序列号
(从 1 开始)为真。这
为每个重置序列号
遇到的范围。 最终序列
范围内的数字包含字符串“E0”
附加到它
,这不影响
它的数值,但给你
如果你想的话可以搜索一些东西
排除端点。您可以排除
起点通过等待
序列号大于1。

如果标量“..”的任一操作数是
常量表达式,该操作数是
如果相等 (==) 则视为 true
到当前输入行号(
$. 变量)。

(强调已添加)

To quote perlop:

In scalar context, ".." returns a
boolean value. The operator is
bistable, like a flip-flop, and
emulates the line-range (comma)
operator of sed, awk, and various
editors. Each ".." operator maintains
its own boolean state, even across
calls to a subroutine that contains
it. It is false as long as its left
operand is false. Once the left
operand is true, the range operator
stays true until the right operand is
true, AFTER which the range operator
becomes false again. It doesn't become
false till the next time the range
operator is evaluated. It can test the
right operand and become false on the
same evaluation it became true (as in
awk), but it still returns true once.
If you don't want it to test the right
operand until the next evaluation, as
in sed, just use three dots ("...")
instead of two. In all other regards,
"..." behaves just like ".." does.

The right operand is not evaluated
while the operator is in the "false"
state, and the left operand is not
evaluated while the operator is in the
"true" state. The precedence is a
little lower than || and &&. The value
returned is either the empty string
for false, or a sequence number
(beginning with 1) for true. The
sequence number is reset for each
range encountered. The final sequence
number in a range has the string "E0"
appended to it
, which doesn't affect
its numeric value, but gives you
something to search for if you want to
exclude the endpoint. You can exclude
the beginning point by waiting for the
sequence number to be greater than 1.

If either operand of scalar ".." is a
constant expression, that operand is
considered true if it is equal (==)
to the current input line number (the
$. variable).

(emphasis added)

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