puzzling behavior of `.[]|=empty`
As far as I understand, the use of empty
in update-assignment |=
deletes the entry, similar to del(path)
, and del(path)
also works in arrays.
I tried to selectively delete and modify array entries, and got strange results. While trying to understand the problem, I minimized the code to this:
Filter: .|=empty
Input: [0,1,2,3,4,5]
Output:[
1,
3,
5
]
Try it online here
Apparently, only even array indexes are deleted. Why?
jq
add a comment |
As far as I understand, the use of empty
in update-assignment |=
deletes the entry, similar to del(path)
, and del(path)
also works in arrays.
I tried to selectively delete and modify array entries, and got strange results. While trying to understand the problem, I minimized the code to this:
Filter: .|=empty
Input: [0,1,2,3,4,5]
Output:[
1,
3,
5
]
Try it online here
Apparently, only even array indexes are deleted. Why?
jq
Interesting. The output injq
1.5 (which until now I thought was the most recent version) isnull
, whilejqplay.org
is using the recently released 1.6 version. I could imagine that|=
andempty
are each consuming their own element from the input (empty
takes 0,|=
takes 1, etc), but whether 1.5 or 1.6 is "correct", I couldn't say. (That is, it could be a bug fix or a regression in 1.6.)
– chepner
Nov 19 '18 at 20:15
add a comment |
As far as I understand, the use of empty
in update-assignment |=
deletes the entry, similar to del(path)
, and del(path)
also works in arrays.
I tried to selectively delete and modify array entries, and got strange results. While trying to understand the problem, I minimized the code to this:
Filter: .|=empty
Input: [0,1,2,3,4,5]
Output:[
1,
3,
5
]
Try it online here
Apparently, only even array indexes are deleted. Why?
jq
As far as I understand, the use of empty
in update-assignment |=
deletes the entry, similar to del(path)
, and del(path)
also works in arrays.
I tried to selectively delete and modify array entries, and got strange results. While trying to understand the problem, I minimized the code to this:
Filter: .|=empty
Input: [0,1,2,3,4,5]
Output:[
1,
3,
5
]
Try it online here
Apparently, only even array indexes are deleted. Why?
jq
jq
asked Nov 19 '18 at 20:05
user368683user368683
1264
1264
Interesting. The output injq
1.5 (which until now I thought was the most recent version) isnull
, whilejqplay.org
is using the recently released 1.6 version. I could imagine that|=
andempty
are each consuming their own element from the input (empty
takes 0,|=
takes 1, etc), but whether 1.5 or 1.6 is "correct", I couldn't say. (That is, it could be a bug fix or a regression in 1.6.)
– chepner
Nov 19 '18 at 20:15
add a comment |
Interesting. The output injq
1.5 (which until now I thought was the most recent version) isnull
, whilejqplay.org
is using the recently released 1.6 version. I could imagine that|=
andempty
are each consuming their own element from the input (empty
takes 0,|=
takes 1, etc), but whether 1.5 or 1.6 is "correct", I couldn't say. (That is, it could be a bug fix or a regression in 1.6.)
– chepner
Nov 19 '18 at 20:15
Interesting. The output in
jq
1.5 (which until now I thought was the most recent version) is null
, while jqplay.org
is using the recently released 1.6 version. I could imagine that |=
and empty
are each consuming their own element from the input (empty
takes 0, |=
takes 1, etc), but whether 1.5 or 1.6 is "correct", I couldn't say. (That is, it could be a bug fix or a regression in 1.6.)– chepner
Nov 19 '18 at 20:15
Interesting. The output in
jq
1.5 (which until now I thought was the most recent version) is null
, while jqplay.org
is using the recently released 1.6 version. I could imagine that |=
and empty
are each consuming their own element from the input (empty
takes 0, |=
takes 1, etc), but whether 1.5 or 1.6 is "correct", I couldn't say. (That is, it could be a bug fix or a regression in 1.6.)– chepner
Nov 19 '18 at 20:15
add a comment |
1 Answer
1
active
oldest
votes
As noted in a comment, jq's treatment of . |= empty
has varied over time. One might expect that for every array, A, A | (. |= empty)
would yield on the theory that the expression should result in every item in A being replaced by
empty
.
In any case, the current (jq 1.6) implementation is clearly wrong, as is particularly noticeable in this example:
jq-1.6 -n '[0,1,2,3] | (. |= if . == 2 then empty else . end)'
[
0,
1,
3,
null
]
Bug reports can be submitted to https://github.com/stedolan/jq/issues
Thanks @peak for the example. I've used a variant in the jq GitHub issue
– user368683
Nov 21 '18 at 7:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381867%2fpuzzling-behavior-of-empty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As noted in a comment, jq's treatment of . |= empty
has varied over time. One might expect that for every array, A, A | (. |= empty)
would yield on the theory that the expression should result in every item in A being replaced by
empty
.
In any case, the current (jq 1.6) implementation is clearly wrong, as is particularly noticeable in this example:
jq-1.6 -n '[0,1,2,3] | (. |= if . == 2 then empty else . end)'
[
0,
1,
3,
null
]
Bug reports can be submitted to https://github.com/stedolan/jq/issues
Thanks @peak for the example. I've used a variant in the jq GitHub issue
– user368683
Nov 21 '18 at 7:33
add a comment |
As noted in a comment, jq's treatment of . |= empty
has varied over time. One might expect that for every array, A, A | (. |= empty)
would yield on the theory that the expression should result in every item in A being replaced by
empty
.
In any case, the current (jq 1.6) implementation is clearly wrong, as is particularly noticeable in this example:
jq-1.6 -n '[0,1,2,3] | (. |= if . == 2 then empty else . end)'
[
0,
1,
3,
null
]
Bug reports can be submitted to https://github.com/stedolan/jq/issues
Thanks @peak for the example. I've used a variant in the jq GitHub issue
– user368683
Nov 21 '18 at 7:33
add a comment |
As noted in a comment, jq's treatment of . |= empty
has varied over time. One might expect that for every array, A, A | (. |= empty)
would yield on the theory that the expression should result in every item in A being replaced by
empty
.
In any case, the current (jq 1.6) implementation is clearly wrong, as is particularly noticeable in this example:
jq-1.6 -n '[0,1,2,3] | (. |= if . == 2 then empty else . end)'
[
0,
1,
3,
null
]
Bug reports can be submitted to https://github.com/stedolan/jq/issues
As noted in a comment, jq's treatment of . |= empty
has varied over time. One might expect that for every array, A, A | (. |= empty)
would yield on the theory that the expression should result in every item in A being replaced by
empty
.
In any case, the current (jq 1.6) implementation is clearly wrong, as is particularly noticeable in this example:
jq-1.6 -n '[0,1,2,3] | (. |= if . == 2 then empty else . end)'
[
0,
1,
3,
null
]
Bug reports can be submitted to https://github.com/stedolan/jq/issues
answered Nov 19 '18 at 21:08
peakpeak
32.3k93959
32.3k93959
Thanks @peak for the example. I've used a variant in the jq GitHub issue
– user368683
Nov 21 '18 at 7:33
add a comment |
Thanks @peak for the example. I've used a variant in the jq GitHub issue
– user368683
Nov 21 '18 at 7:33
Thanks @peak for the example. I've used a variant in the jq GitHub issue
– user368683
Nov 21 '18 at 7:33
Thanks @peak for the example. I've used a variant in the jq GitHub issue
– user368683
Nov 21 '18 at 7:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381867%2fpuzzling-behavior-of-empty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Interesting. The output in
jq
1.5 (which until now I thought was the most recent version) isnull
, whilejqplay.org
is using the recently released 1.6 version. I could imagine that|=
andempty
are each consuming their own element from the input (empty
takes 0,|=
takes 1, etc), but whether 1.5 or 1.6 is "correct", I couldn't say. (That is, it could be a bug fix or a regression in 1.6.)– chepner
Nov 19 '18 at 20:15