Table & Dictionary

✏ Beginners Scripting Guide ✏

In this section, we will be discussing tables and dictionaries. A table can be used to store data inside and a dictionary is a key with a value in the table.

CREATING A TABLE

First, we are going to make a variable and we gonna store curly brackets in that variable

local myTable = {} -- Create a new empty table

PUTTING DATA INTO THE TABLE

Now, we are going to put some data into a table. In this example, I will be using string.

local myTable = {
     'Banana';
     'Apple';
     'Orange';
}

GETTING DATA INSIDE THE TABLE

To get the data inside the table, we have to index it out by getting the table, using the square brackets, and indexing it out with numbers.

local myTable = {
     'Banana'; -- 1
     'Apple'; -- 2
     'Orange'; -- 3
}

print(myTable[1]) -- This will print out Banana

GETTING THE TABLE LENGTH

To get the amounts of how many arrays are in the table. We can use the # symbol to see the number of arrays in the table.

local myTable = {
	'Banana'; -- 1
	'Apple'; -- 2
	'Orange'; -- 3
}

print(#myTable) -- This will print 3 because there are 3 arrays in the table.

CREATING A DICTIONARY

To make a dictionary we will create a new table.

local myDictionary = {}

INSERTING KEYS

Now we will give the key a name and assign data to it.

local myDictionary = {
     Fruit1 = 'Apple';
     Fruit2 = 'Orange';
     ['Best Fruit'] = 'Grapes'
}

GETTING THE KEY DATA

Now, we can get the data by using the dictionary, using square brackets or a period, and then indexing the key name.

local myDictionary = {
     Fruit1 = 'Apple';
     Fruit2 = 'Orange';
     ['Best Fruit'] = 'Grapes'
}

print(myDictionary.Fruit1) -- Apple
print(myDictionary['Best Fruit']) -- Grapes

Last updated