diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index ae7a9d39..8eb1a0c4 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -2818,8 +2818,12 @@ sections: not a valid or useful path expression in `.`); use `$var | .foo |= . + 1` instead. - If the right-hand side outputs multiple values, only the last - one will be used. + If the right-hand side outputs no values (i.e., `empty`), then + the left-hand side path will be deleted, as with `del(path)`. + + If the right-hand side outputs multiple values, only the first + one will be used (NOTE: it used to be that only the last one was + used). examples: diff --git a/jq.1.prebuilt b/jq.1.prebuilt index 615b033c..37156d4f 100644 --- a/jq.1.prebuilt +++ b/jq.1.prebuilt @@ -3033,7 +3033,10 @@ The left\-hand side can be any general path expression; see \fBpath()\fR\. Note that the left\-hand side of \'|=\' refers to a value in \fB\.\fR\. Thus \fB$var\.foo |= \. + 1\fR won\'t work as expected (\fB$var\.foo\fR is not a valid or useful path expression in \fB\.\fR); use \fB$var | \.foo |= \. + 1\fR instead\. . .P -If the right\-hand side outputs multiple values, only the last one will be used\. +If the right\-hand side outputs no values (i\.e\., \fBempty\fR), then the left\-hand side path will be deleted, as with \fBdel(path)\fR\. +. +.P +If the right\-hand side outputs multiple values, only the first one will be used (NOTE: it used to be that only the last one was used)\. . .IP "" 4 . diff --git a/src/builtin.jq b/src/builtin.jq index d6cd4321..f7c6e9b7 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -10,7 +10,7 @@ def min_by(f): _min_by_impl(map([f])); def add: reduce .[] as $x (null; . + $x); def del(f): delpaths([path(f)]); def _assign(paths; value): value as $v | reduce path(paths) as $p (.; setpath($p; $v)); -def _modify(paths; update): reduce path(paths) as $p (.; setpath($p; getpath($p) | update)); +def _modify(paths; update): reduce path(paths) as $p (.; label $out | (setpath($p; getpath($p) | update) | ., break $out), delpaths([$p])); def map_values(f): .[] |= f; # recurse