range营运人,三功能变量
perl -wne /abc/ ... /abc/ and print data.txt
Another way, with an explicit flag and all but more concise
perl -wnlE /abc/ and $f ^= 1; $f and say data.txt
This doesn t print the end marker though. To have both start and end markers printed
perl -wnlE ($f or /abc/) and say; /abc/ and $f ^= 1 data.txt
解释——
In Perl all logical营运人. Short-range。 审议<代码>A和B:如果第一种表述(A
)对“falsey”进行评价,则<代码>B即为未评价<>/em>——该代码不适用。 因此,<代码>A和B主要相当于if (A){B }
。
I use the short-circuiting nature here to streamline code for a one-liner; it s normally far clearer in normal code to write it out nicely. So, the first statement amounts to
if ($f or /abc/) { say }
-- print the line if $f
evaluates to "truthy" (flag is set) or we are on the line with abc
. The second condition is matched by regex, $_ =~ m/abc/
, where m
may be omitted with //
delimiters and the pattern binds to $_
by default so that can be omitted as well -- for /abc/
, which returns true/false.
Now for setting that flag...
接下来的发言,我们测试了/abc/
,如果-it-matches-then (short-rangeing and
),我们做的是$f ^= 1
。 第二种表述使用如下的双目<代码>^。
When two numbers are bitwise XOR-ed each pair of their bits is XOR-ed -- the resulting bit is set if one of them is set but not the other, and it s not set otherwise. So 0101 ^ 1100
gives 1001
(higher four bits omitted here; needed for testing)
Then doing it with 1
results in flipping the lowest bit: 6 ^ 1
produces 7 (0110 ^ 1
--> 0111
) while 7 ^ 1
returns 6
. And we flip between 0
and 1
. Then we assign that back, $f = $f ^ 1;
for which there is syntax $f ^= 1
.
因此,如果该国旗为未定(0
--->1
,则以与abc
的行文为准,则按下行的需要设定。
当然,从文档和线末线上读到的斜线,可以使用<条码><>>><>/条码>,而不是<条码>:<>条码/代码>,然后只需要<条码>-wne条码>转换。
I just liked say
better here. Also, -lE
with say
handles a case where this filter is fed strings wihtout linefeeds, as well.