Square brackets
Square brackets are used in MiniScript to index into a value — that is, to select just one element out of a collection of elements. They are also used to slice lists and strings.
List Indexing and Slicing
Given a list a
, square brackets can be used in several ways:
a[i]
gets element i of the list. If i is 0 or more, then this is the element with i elements to its left: the first one isa[0]
, the next one isa[1]
, and so on. If i is negative, then it counts from the end of the list:a[-1]
gets the last element,a[-2]
gets the next-to-last one, etc.
a[i] = x
replaces element i of the list with a new value x. The meaning of i here is exactly the same as above.
a[i:j]
gets a new list with elements made up a sub-range of a: specifically, element i up to (but not including) element j. The meaning of both i and j is as above. (This operation is known as slicing the list, and the resulting copy is sometimes called a slice.)
a[i:]
gets a new list made of elements i through the end of the list.
a[:i]
gets a new list made of elements from the beginning of the list up to (but not including) element i.
a[:]
gets a new list containing all the same elements of a (and is a handy way to copy a list, in case you want to change the copy without affecting the original list).
Note that you cannot assign to a slice of a list. To replace a sub-range of a list you will need to add (concatenate) the part before the sub-range, your new middle, and the part after the sub-range.
String Indexing and Slicing
Given a string s
, square brackets can be used in several ways:
s[i]
gets character i of the string. If i is 0 or more, then this is the character with i characters to its left: the first one iss[0]
, the next one iss[1]
, and so on. If i is negative, then it counts from the end of the string:s[-1]
gets the last character,s[-2]
gets the next-to-last one, etc.
s[i:j]
gets a new string with characters made up a sub-range of s: specifically, character i up to (but not including) character j. The meaning of both i and j is as above. (This operation is known as slicing the string, and the resulting copy is sometimes called a slice.)
s[i:]
gets a new string made of characters i through the end of the list.
s[:i]
gets a new string made of characters from the beginning of the string up to (but not including) character i.
s[:]
gets a new string containing all the same characters of n (but since strings are immutable, this is generally a pointless thing to do).
Note that because strings are immutable (cannot be changed), you cannot replace an element of a string via indexing. This is unlike lists or maps, which are mutable (can be changed).
Map Indexing
Given a map m
, square brackets can be used in these ways:
m[k]
gets the value in the map associated with key k. (You may want to first check whether that key exists using hasIndex.)
m[k] = x
stores value x in the map, associated with key k. This replaces the previous value, if any, associated with k.
Note that in the context of a map, the term "index" and "key" are synonyms. But intrinsic functions like hasIndex and indexOf always refer to them as indexes, since those functions also apply to lists and strings.