HOOKSCRIPTv0.9.1

LISTS

In HookScript, lists are a container type that allow you to store multiple values in a single variable. To create a list, you place your comma separated values between square brackets [value1, value2, value3, ...]:

var x list = [1, 2, 3]
print(len(list)) // returns 3

Retrieving Values by Index

To retrieve a value from a list, use square brackets with the index of the element (e.g., [0]). An error is thrown if the index is out of range. Indexes are zero-based (e.g., [0] references the first element in your list) and can be negative (e.g., [-1] references the last index in your list)

list[0] // returns 1
list[-1] // returns 3

Assigning Values by Index

To assign a value to a list, use square brackets with the index of the element (e.g., [0]). An error is thrown if the index is out of rnage. Indexes start at 0

list[0] = 10

Checking if list contains a value

To check if a list contains a value, use the in operator which will return true or false.

var myList list = [10, 20, 30]
30 in myList // returns true

List Types

Lists can hold any type, including other lists. To assign a type to a list, place the type between < and > characters:

var x = [1, "hello", true, 3.14]
var x list = [1, "hello", true, 3.14]
var x list<int> = [1, 2, 3]
var x list<float> = [1.1, 2.2, 3.3]
var x list<bool> = [true, true, false]
var x list<list<int>> = [ [1, 2, 3], [4, 5, 6 ] ]

When your list contains other lists, you use multiple indexes to retrieve and assign values:

var x list<list<int>> = [ [1, 2, 3], [4, 5, 6] ]
x[1][2] // returns the 3rd element in the 2nd list, the value 6

List methods

Lists come with built-in methods for common operations

  • push(...value): allows you to insert value(s) to the back of the list
  • prepend(...value): allows you to insert value(s) to the front of the list
  • pop(): removes the last item in the list; returns null if list empty

Loading Playground

Built with in Halifax, Nova Scotia by JW