Generic Lists

< 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.


Please select your language

The website is currently localized into the following languages