Tinderbox v9 Icon

Square brackets: list indexes

A list comprises a a number of different items. Often it is useful to address (call for) a list item by its number within the list. For example, historically this was done using .at():

$MyItem = $MyList.at(2); 

This returns item number three in the list, as the count used is 'zero-based', i.e. it starts at zero rather than one.

Another, and more current way is to use [N] directly after the object addressed. Thus, and functionally the same as above:

$MyItem = $MyList[2]; 

This also makes it easier to address values in nested lists. Thus in this list:

$MyList = [4;5;[6;2;4];9;7]; 

the third list item is itself a list. To address a value within that nested lest, append another set of [] like so:

$MyItem = $MyList[2][1]; 

returns '2', the first [2] is item #3 in the main list (i.e. 6;2;4) and then [1] calls the second item in that nested list, returning 2.