puzzling behavior of `.[]|=empty`












3















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?










share|improve this question























  • 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


















3















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?










share|improve this question























  • 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
















3












3








3








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 20:05









user368683user368683

1264




1264













  • 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



















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














1 Answer
1






active

oldest

votes


















1














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






share|improve this answer
























  • Thanks @peak for the example. I've used a variant in the jq GitHub issue

    – user368683
    Nov 21 '18 at 7:33













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
});


}
});














draft saved

draft discarded


















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









1














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






share|improve this answer
























  • Thanks @peak for the example. I've used a variant in the jq GitHub issue

    – user368683
    Nov 21 '18 at 7:33


















1














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






share|improve this answer
























  • Thanks @peak for the example. I've used a variant in the jq GitHub issue

    – user368683
    Nov 21 '18 at 7:33
















1












1








1







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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






















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)