Arrays

Arrays are used to store multiple values in a single variable, making it easier to manage and manipulate data in your script. In HaasScript, there are two ways to manipulate arrays: using Lua-based indexing and table functions or using built-in array commands.

Creating an Array

To create an array, you can simply assign a list of values to a variable, like this:

myArray = {1, 2, 3, 4, 5}

This creates an array of integers with five elements.

Accessing Array Elements

To access the elements of an array, you can use Lua-based indexing. For example, to access the first element of the array created above, you would use:

firstElement = myArray[1]

Modifying Array Elements

You can modify the elements of an array using Lua-based indexing as well. For example, to change the value of the second element of the array created above to 10, you would use:

myArray[2] = 10

Array Commands

HaasScript also includes built-in array commands that you can use to manipulate arrays. Here are a few examples:

ArrayAdd

The ArrayAdd command adds a new element to the end of an input array and returns it.

myArray = {1, 2, 3}
newArray = ArrayAdd(myArray, 4)
-- newArray now contains {1, 2, 3, 4}

ArrayUnshift

The ArrayUnshift command adds a new element to the beginning of an input array and returns it.

myArray = {1, 2, 3}
newArray = ArrayUnshift(myArray, 0)
-- newArray now contains {0, 1, 2, 3}

ArrayConcat

The ArrayConcat command concatenates two arrays together and returns the resulting array.

array1 = {1, 2, 3}
array2 = {4, 5, 6}
newArray = ArrayConcat(array1, array2)
-- newArray now contains {1, 2, 3, 4, 5, 6}

ArrayDistinct

The ArrayDistinct command returns a new array with only unique elements. In other words, it removes all duplicates.

myArray = {1, 2, 3, 2, 4, 4, 5}
distinctArray = ArrayDistinct(myArray)
-- distinctArray now contains {1, 2, 3, 4, 5}

ArrayGet

The ArrayGet command gets the value at a specific index in an array. A negative index will take from the end of the array.

myArray = {1, 2, 3, 4, 5}
thirdElement = ArrayGet(myArray, 3)
-- thirdElement now contains 3

ArrayContains

The ArrayContains command returns true if the array contains any element or an element matches a specific value.

myArray = {1, 2, 3, 4, 5}
containsThree = ArrayContains(myArray, 3)
-- containsThree now contains true

Sure, here’s a section you can add to your course page about Arrays:

LUA Table Functions

HaasScript allows users to use common Lua table functions to manipulate arrays. Here are a few commonly used table functions:

ipairs()

ipairs() is used to iterate over an array in numerical order.

Example:

myArray = {"apple", "banana", "cherry"}

for index, value in ipairs(myArray) do
    Log(index .. ' ' .. value)
end

Output:

1 apple
2 banana
3 cherry

pairs()

pairs() is used to iterate over an array in arbitrary order.

Example:

myArray = {"apple", "banana", "cherry"}

for index, value in pairs(myArray) do
    Log(index .. ' ' .. value)
end

Output:

1 apple
2 banana
3 cherry

table.insert()

table.insert() is used to insert a new element into an array at a specified index.

Example:

myArray = {"apple", "banana", "cherry"}
table.insert(myArray, 2, "orange")

Output:

{"apple", "orange", "banana", "cherry"}

table.remove()

table.remove() is used to remove an element from an array at a specified index.

Example:

myArray = {"apple", "banana", "cherry"}
table.remove(myArray, 2)

Output:

{"apple", "cherry"}

Conclusion

Arrays are a powerful tool for managing and manipulating data in your HaasScript scripts. With Lua-based indexing, table functions and built-in array commands, you have a variety of options for working with arrays in your scripts.

Back to: HaasScript Fundamentals > Using Data