Using arrays
In this guide you will learn what arrays are, and how to use them.
General info
Generally speaking, arrays are collections of elements arranged in order.
You can think of them as lines/queues containing multiple people(elements).
Indexing
Indexes are used to differentiate elements in most array-related functions.
Let's say we had an array of three people: Bob, Adam, and Lucy.
- Bob's index is 1
- Adam's index is 2
- Lucy's index is 3
Once you have an array, you can use a variety of functions to edit and get information from it.
Using arrays
In Custom Command there are two main ways of creating an array. In this guide, we will cover both of them.
1. Text splitting
To use an array you first have to create one, the most popular way is with $arrayCreate function. It takes a certain text, and splits it by a separator (any text that separated the elements).
Example
Let's say we have a text apple, banana, orange
, and we want to turn it into an array. As you can see, these names are separated by a comma ,
.
$arrayCreate doesn't return anything itself, we need a different function to check if it works. To check if everything works properly, we will read the number of elements with $arrayLength.
// Create an array of three elements
$arrayCreate[apple, banana, orange;, ]
// Get the first element
$arrayLength
Let's execute the code, and check how many elements have been created.
As you can see, the number returned matches with the number of fruits we provided.
2. Pushing elements
You may not always want to create an array from a given text. In some cases, you may prefer to start building an array, by appending new elements to an empty one.
In order to do that, you can use the $arrayPush function, which adds a new element to the end of the array.
Let's craft an array of a few different animals, and check what's the first and last one.
// Push all thre elements
$arrayPush[Dog]
$arrayPush[Cat]
$arrayPush[Mouse]
$arrayPush[Elephant]
// Remove and return the last element
$arrayGet[1], $arrayGet[4]
Let's execute this code and see what it returns.
As you can array push has correctly registered separate animals.
3. Searching for values
As you may know, array functions mostly work based on element indexes. Generally, this kind of behavior is convenient, but we also need a way to get those indexes from existing arrays.
Here's how to use $arraySearch to do just that.
Let's start off by creating a new array of a few staff members, and get fajfaj
's index.
$arrayCreate[RAKE Oanretesue Mido fajfaj ThatKev; ]
fajfaj's index is `$arraySearch[fajfaj]`
Now, head back to discord, and let's check the output:
4. Saving arrays
By default, all changes made to an array are reset right after execution. However, you may want to use arrays to store some data across executions/commands, that has to keep track of all changes among the executions.
In that case permanent variables come in handy.
In this example we will create a !todo
command that will store and display all tasks scheduled. More specifically, we want it to:
// Initiate a variable with empty array
$initVar[server;todo;]
// Load the array from variable (Using | as separator)
$arrayCreate[$getServerVar[todo];|]
// Push a task to the list
$arrayPush[$message]
// Save the array (Using | as separator)
$setServerVar[todo;$arrayJoin[|]]
// Format the list to "1. task" (optional)
$arrayMap[value;index]{$index. $value}
// Return the list
TODO:
$arrayJoin[#NL#]
Let's add some tasks
Notice that changes made to the array do not expire after execution.
🎉 Congratulations
You've reached the end of this tutorial, but not the end of array capabilities! There's plenty of functions that make arrays tick, have fun exploring the Array category!