< 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.
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”.
©2024 Boris FX, Inc. — UNOFFICIAL — Converted from original PDF.