开发者

What does a { at the end of a Perl one-liner mean?

I've seen this one-liner

perl -lane '$_{$F[0]}+=$F[1]}print"$_ $_{$_}"for keys%_;{' file

here: How can I sum values in column based on the value in another column?

and I don't remember how the "开发者_如何学Go{" at the end works. Could someone explain how it works?


From the Perl help:

-n   assume "while (<>) { ... }" loop around program

This is purely a textual operation, so it gives this program:

while (<>) { $_{$F[0]}+=$F[1]}print"$_ $_{$_}"for keys%_;{ }

This is an abuse of the -n switch because the while loop is closed early due to the unmatched } in the original program. But the closing } that is added by the -n switch still needs to match with something, and that's why there needs to be an extra { at the end of the program, even though it doesn't do anything.

In other words, the only reason the last { is there is to not give a syntax error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜