Skip to content

Commit

Permalink
Improve assignment docs (see #897)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams committed Aug 11, 2015
1 parent 9a3f802 commit c426d49
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions docs/content/3.manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,9 @@ sections:
of every object before it does the assignment (for performance,
it doesn't actually do that, but that's the general idea).
All the assignment operators in jq have path expressions on the
left-hand side.
entries:
- title: "`=`"
body: |
Expand All @@ -2651,9 +2654,20 @@ sections:
can produce can be represented in JSON.
Note that the left-hand side of '=' refers to a value in `.`.
Thus `$var.foo = 1` won't work as expected; use `$var | .foo =
Thus `$var.foo = 1` won't work as expected (`$var.foo` is not
a valid or useful path expression in `.`); use `$var | .foo =
1` instead.
If the right-hand side of '=' produces multiple values, then
for each such value jq will set the paths on the left-hand
side to the value and then it will output the modified `.`.
For example, `(.a,.b)=range(2)` outputs `{"a":0,"b":0}`, then
`{"a":1,"b":1}`. The "update" assignment forms (see below) do
not do this.
Note too that `.a,.b=0` does not set `.a` and `.b`, but
`(.a,.b)=0` sets both.
- title: "`|=`"
body: |
As well as the assignment operator '=', jq provides the "update"
Expand All @@ -2677,9 +2691,13 @@ sections:
The left-hand side can be any general path expression; see `path()`.
Note that the left-hand side of '|=' refers to a value in `.`.
Thus `$var.foo |= . + 1` won't work as expected; use `$var |
Thus `$var.foo |= . + 1` won't work as expected (`$var.foo` is
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.
examples:

- program: '(..|select(type=="boolean")) |= if . then 1 else 0 end'
Expand Down

0 comments on commit c426d49

Please sign in to comment.