Tables
As you learned in Special types, the basic data types Powerlang supports are numbers, strings and booleans. However, there may be a need to store one or multiple instances of these types in a container or list. For instance, you may want to store a list of strings representing usernames of admins in your game, or a list of numbers representing high scores. That's where tables are used - they provide a way to store multiple objects as one variable.
Tables are created in Powerlang using the following syntax:
This will create a table with 3 elements: 1, 2 and 3, and assign it to the variable myTable. Note that tables are not limited to numbers, tables can contain any amount of any data type, including special types.
Powerlang tables differ a lot from Lua tables. If you are coming from Lua, you might find information below helpful.
In Lua, tables are defined using the {}
syntax, and the above example would look like myTable = {1, 2, 3}
. However, in Powerlang, curly braces are used to denote code, not tables, therefore we must use table.create()
instead.
In Lua, table.create(n, obj)
creates a table with n
occurences of obj
. In Powerlang, table.create(...)
creates a new table with the elements inside...
.
Table operations
Creating a table is pointless if you're not going to use it. Therefore let's take a look at some basic operations one can do with a table.
To set a table's value, we can use the []
operator:
This will set the first element (index 1) in the table to be the string "bananas". You aren't limited to just setting the first element, you can set any element you wish. In both Powerlang and Lua, the first table element has the index of 1.
Note that you can set an element to a negative, float or non-number index, in which case a dictionary will be used instead. Dictionaries are discussed later on this page.
To get a table's value, we can also use the []
operator likewise:
This will print the first element in the table. As per the previous example, this would be "bananas".
You aren't limited to just getting and setting values however! The table
library contains lots of useful features for working with tables. You might be familiar with table
already, as we used table.create
in the beginning of this article to create a table. You will learn more about libraries in Libraries.
To insert an item anywhere in a table, we can use table.insert
like this:
This will insert the string "bananas" in the second index of the table.
Note the difference:
myTable[2] = "bananas"
would overwrite the second element in the table with "bananas".
table.insert(myTable, "bananas", 2)
would insert "bananas" as the second element and move other elements to fit.
To get the amount of items in a table, we can use table.length
:
If we follow our first example and consider myTable
as 1, 2, 3
, then its length will be 3.
To remove an item from a table, we can use table.remove
:
This will remove the second element from myTable
and prevent a "gap" from forming by pushing all following elements 1 index backwards. Therefore, the table would become 1, 3
.
You can check out more interesting functions the table
library has to offer in its documentation.
Dictionaries
It is not necessary to use a number index when assigning an item. You can use a string, for example:
This will assign "bananas" to a key in the table (or dictionary, in this case) myTable
, then access the key again and print the contents. A table can be both an array (list of items) and a dictionary (key-item pairs) at the same time. However, the table
library's methods will not work with dictionaries, therefore you can only get and set dictionary keys, and nothing else. If your table consists of both a list and a dictionary, table
functions will still work with the list portion of the table.
Hopefully, tables are clear to you. Let's move on to loops.
Last updated