Next: , Previous: , Up: Arrays   [Contents][Index]


10.4.5 Array Trimming

Indexing is used to fetch elements from arrays. Another operation, called trimming, allows you to extract a subset of the array, as another array.

Trims use the following notation, where a range is specified between square brackets. Both sides of the range are included:

(poke) [1,2,3][0:1]
[1,2]
(poke) [1,2,3][1:1]
[2]
(poke) [1,2,3][0:2]
[1,2,3]

If the minimum side of the range is omitted, it is assumed to be zero. If the maximum side of the range is omitted, it is assumed to be the length of the trimmed array minus one:

(poke) [1,2,3][:1]
[1,2]
(poke) [1,2,3][1:]
[2,3]
(poke) [1,2,3][:]
[1,2,3]

The elements of the base array and the trimmed sub-array are copied by shared value, exactly like when passing arguments to functions. This means that for simple types, copies of the elements are done:

(poke) defvar a = [1,2,3]
(poke) defvar s = a[1:1]
(poke) s[0] = 66
(poke) a
[1,2,3]

However, for complex types like arrays and structs, the values are shared:

(poke) defvar a = Packet[] @ 0#B
(poke) defvar s = a[1:1]
(poke) s[0].field = 66
(poke) a[1].field
66