Jq 涉及左侧变量的管道的奇怪行为
我遇到了 jq
的奇怪行为,涉及管道左侧的变量。
供您参考,这个问题的灵感来自 jq
手册:在 Scoping
下 (https://stedolan.github.io/jq/manual/#Advancedfeatures) 其中提到了一个示例过滤器 ... | .*3 为 $times_ Three | [。 + $times_ Three] | ...
。我相信正确的版本是 ... | (.*3) 作为 $times_ Three | [。 + $times_ Three] | ...
。
第一个 (https://jqplay.org/s/ffMPsqmsmt)
filter:
. * 3 as $times_three | .
input:
3
output:
9
第二个 (https://jqplay.org/s/yOFcjRAMLL)
filter:
. * 4 as $times_four | .
input:
3
output:
9
发生了什么 这里?
但是(https://jqplay.org/s/IKrTNZjKI8)
filter:
(. * 3) as $times_three | .
input:
3
output:
3
并且(https://jqplay.org/s/8zoq2-HN1G)
filter:
(. * 4) as $times_four | .
input:
3
output:
3
因此,如果括号 (.*3)
或 (.*4)
是在声明变量时使用,然后过滤器的行为可预测。
但如果不使用括号 .*3
或 .*4
,那么奇怪的是,两者的输出都是 9。
你能解释一下吗?
I came across a weird behavior of jq
involving a variable on the left hand side of a pipe.
For your information, this question was inspired by the jq
manual: under Scoping
(https://stedolan.github.io/jq/manual/#Advancedfeatures) where it mentions an example filter ... | .*3 as $times_three | [. + $times_three] | ...
. I believe the correct version is ... | (.*3) as $times_three | [. + $times_three] | ...
.
First (https://jqplay.org/s/ffMPsqmsmt)
filter:
. * 3 as $times_three | .
input:
3
output:
9
Second (https://jqplay.org/s/yOFcjRAMLL)
filter:
. * 4 as $times_four | .
input:
3
output:
9
What is happening here?
But (https://jqplay.org/s/IKrTNZjKI8)
filter:
(. * 3) as $times_three | .
input:
3
output:
3
And (https://jqplay.org/s/8zoq2-HN1G)
filter:
(. * 4) as $times_four | .
input:
3
output:
3
So if parenthesis (.*3)
or (.*4)
is used when the variable is declared then filter behaves predictably.
But if parenthesis is not used .*3
or .*4
then strangely the output is 9 for both.
Can you explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与范围界定部分中的示例所假设的相反,
。 * 4 为 $times_four | 。
相当于。 * ( 4 as $times_four | . )
因此对其输入进行平方。您可能期望
等同于
正如您所指出的,一些示例甚至表明情况确实如此。然而,第一个片段实际上相当于以下内容:
相同
并且由于
... as $x
生成其上下文[1],因此与orjq 的运算符优先级不一致和/或古怪。
“定义”| “abc”+“def”| length
表示"def" | (“abc”+“def”) |长度
,但是"def" | “abc”+“def”为 $x | length
表示"def" | “abc”+(“def”为 $x | 长度)
。这种行为表明
as
并不是人们所期望的X as $Y
形式的二元运算符,而是X as 形式的三元运算符$Y | Z。
事实上,它的记录方式是这样的:
这会带来惊喜,特别是因为它的结合比预期的要紧密得多。看起来范围界定部分中示例的作者都落入了陷阱。
.[] as $x
。Contrary to what the examples in the Scoping section assume,
. * 4 as $times_four | .
is equivalent to. * ( 4 as $times_four | . )
and therefore squares its input.You might expect
to be equivalent to
And as you point out, some example even suggest this is the case. However, the first snippet is actually equivalent to the following:
And since
… as $x
produces its context[1], that's the same asor
jq
's operator precedence is inconsistent and/or quirky."def" | "abc" + "def" | length
means"def" | ( "abc" + "def" ) | length
, but"def" | "abc" + "def" as $x | length
means"def" | "abc" + ( "def" as $x | length )
.This behaviour suggests that that
as
isn't a binary operator of the formX as $Y
as one might expect, but a ternary operator of the formX as $Y | Z
.And, in fact, this is how it's documented:
This leads to surprises, especially since it binds a lot more tightly than expected. And it looks like whomever authored the examples in the Scoping section fell into the trap.
.[] as $x
.确实,说明书上好像有错误。在 范围界定 部分中,它对比了(错误的)示例
和
虽然总体声明保持不变有效,两个示例都缺少
.*3
周围的附加括号。因此,它实际上应该分别读取和
。
从手册下的部分 变量/ 符号绑定运算符:
这意味着变量赋值采用
as
左侧的 one 表达式,并将其计算值分配给as
右侧定义的变量(这发生为exp
产生输出时多次)。但是,由于 jq 中的所有内容都是过滤器,因此赋值本身也是过滤器,因此它本身需要有一个输出。如果你仔细观察,该部分的完整标题旁边还有一个管道符号,表示它属于分配的结构。尝试只运行
。如 $x
。您将收到错误,因为| ...
部分丢失。因此,为了简单地保持输入上下文不变(除了可能将其复制与as
左侧的表达式产生输出一样多的次数),完整的赋值宁愿看起来像... as $ x| .
,或者,如果输入上下文是您想要在变量中捕获的内容。作为 $x | .
也就是说,让我们通过在赋值周围放置显式括号来澄清示例中发生的情况:
Indeed, there seems to be a mistake in the manual. In section Scoping it is contrasting the (faulty) examples
and
While the overall statement stays valid, both examples are missing additional parentheses around
.*3
. Thus, it should actually readand
respectively.
From the manual under section Variable / Symbolic Binding Operator:
This means that a variable assignment takes the one expression left of
as
and assigns its evaluation to the defined variable right ofas
(and this happens as many times asexp
produces an output). But, as everything in jq is a filter, the assignment itself also is, and as such it needs to have an output itself. If you look closely, the full title of that sectionalso features a pipe symbol next to it, which indicates that it belongs to the assignment's structure. Try just running
. as $x
. You will get an error because the| ...
part is missing. Thus, to simply keep the input context as is (apart from maybe duplicating it as many times as the expression left ofas
produced an output), a complete assignment would rather look like… as $x | .
, or, if the input context is what you wanted to capture in the variable,. as $x | .
That said, let's clarify what happens with your examples by putting explicit parentheses around the assignments: