< Previous | Contents | Manuals Home | Boris FX | Next >
โGeneric Lists
You can create generic indexed lists of objects
Mylist = [1, 2, โbuckle my shoeโ]
and subsequently change or retrieve the components with Mylist[2], for example. You can tack on additional items to the last slot, with Mylist[4] = 3, Mylist[5]=4, etc. It is an error to perform Mylist[10] = โwhy?โ because that would leave a hole.
You can create a new empty list to add items to
MyNewEmptyList = [ ]
You can also create associative lists with
Color[โappleโ] = โredโ Color[โpearโ] = โgreenโ
You can access Color[2] as well, at this point. #Color is the number of elements, 2 in this example
Color.isKeyed is the number of keys actually set in the array; values
added with no key arenโt counted.
Color.keys is the list [โappleโ, โpearโ]. Note that there may be fewer keys than elements in the list, if some elements have been added without keys, using numeric subscripts.
Color.key(2) = โpearโ, ie the key for the nth item in the list, or a null if element has no key.
Color.index(โpearโ) = 2, ie the index of a key in the list, or 0 if it cannot be
found.
Color.contains("peach") = 0, ie false.
Color.remove(โpearโ) or Color.remove(2) to remove a list element. Be
aware that Sizzle subscripting creates copies of the list, so you canโt directly remove an element from a list inside another list. See the example at the end of this section.
Sizzle lets you create JSON-style hierarchical lists with optional fixed keys: the keys must be string constants, not an expression in its own right. Thereโs no need/desire to distinguish between keyed and unkeyed lists here, so an example looks like this:
obj = [
"answer" : 42, "isBoolean" : true, "sequence" : [2, 3, 4], "errors" : null, "nested" : [
"hummingbird" : "ruby", "sticks": 39,
"finished" : false
],
"unknown" : "whether this will be helpful"
]
Scene.JSONwrite("C:/tmp/handmade.json", obj)
As you can see, the produced object can be written to a file to produce this output:
{
"answer": 42, "isBoolean": true, "sequence": [
2,
3,
4
],
"errors": null, "nested": {
"hummingbird": "ruby", "sticks": 39, "finished": false
},
"unknown": "whether this will be helpful"
}
There are complementary functions JSONread, JSONencode (to a string, not a file), and JSONdecode (a string to a potentially-complex nested object).
Note the presence of true, false, and null entries in the input and output; they are all preserved. The Booleans true and false are variables containing the corresponding special Boolean value. They behave as numbers in almost all contexts. To determine if a value read in is a number or a Boolean, use typeof(value) to produce either โBooleanโ or โDoubleโ.
With the โobjโ variable listed above, you cannot do this:
obj["nested"].remove("sticks")
because โsticksโ is removed from the copy of obj[โnestedโ]. Instead, do this:
nested = obj["nested"] nested.remove("sticks") obj["nested"] = nested
This applies to attribute assignment as well, and may be addressed in some future release.
ยฉ2025 Boris FX, Inc.ย โย UNOFFICIALย โย Converted from original PDF.