Array: Difference between revisions

(jq)
Line 63:
 
====Immutability====
jq offers a comprehensive collection of operators and functions for array processing, but to understand them it must be appreciated that all data values in JSON are immutable. In particular, if a is an array, a jq expression such as "a[0] = 1" may give the appearance of updating the array, but in fact it simply returns an array identical to <tt>a</tt> except for the first element.
 
In fact, an expression such as "a[m] = 1" (where m is some non-negative integer) does not require that the length of the array, a, be at least (m+1). The expression should instead be interpreted to mean: produce an array derived from a such that a[m] == 1. If necessary, jq will add "null" elements to achieve this requirement. Thus, one way to create an array of m nulls is to write:<lang jq>[][m]</lang>
 
In fact, an expression such as "a[m] = 1" (where m is some non-negative integer) does not require that the length of the array, a, be at least (m+1). The expression should instead be interpreted to mean: produce an array derived from <tt>a</tt> such that a[m] == 1. If necessary, jq will add "null" elements to achieve this requirement. Thus, one way to create an array of m nulls is to write:<lang jq>[][m]</lang>
It might seem that jq must be horrendously inefficient because of immutability, but in fact, jq is fast because of compiler optimizations.
 
====Basic Array Operations====
Assuming a and b are arrays, and if m and n are non-negative integers:
2,511

edits