Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Questions regarding these types of tables?

Asked by 4 years ago

(This is a question, I do not have a script to include.)

I just started learning tables. I know the basics of tables and how they store different values, and I also know how to use table.insert and table.remove and I know how to loop through them. What I want to know is how to use the tables that store data inside of the entry. For example:

local mytable = {["Player1"] = 5,["Player2"] = 7,["Player3"] = 9}

I do know that you can retrieve the data of one entry by doing this:

local val = mytable["Player1"]

I find these tables being very useful for maybe a leaderboard displaying player kills or a map voting system, or even to store multiple values in one DataStore key.

So, how do I add an entry to these tables? because table.insert(mytable,["Player4"]) will not do it.

Also, I know table.sort will sort these tables and put the highest number at index number 1, but I also do not know how to use that and need help with that as well.

Also, correct me for any mistakes I made.

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

Check out my in-depth explanation of table.insert and table.remove here.

This type of structure for a table is called a dictionary, and the other {1, 2, 3, ...} is called an array (or list). You should only use an array when you care about the position of the elements in the table, or just a set of values.

You can only use table.insert with arrays because the function allows you to insert elements anywhere in the table while keeping all of it's indices in numeric order. You cannot using table.insert or table.remove with dictionaries because they don't have any order to them. They just hold key/value pairs. To get or set these key/value pairs, we index the table with a string of the key name, using square brackets or a period:

local dict = {a = 1, b = 2, c = 3}

print( dict.a ) --> 1
print( dict["a"] ) -->1

What's the difference? We only need the square brackets if the index key is a variable, or if the name of the key has any characters that go against the syntax rules of a variable name.

local dict = { ["letter a"] = 1, ["letter b"] = 2, ["letter c"] = 3 }

-- Incorrect. Against syntax rules; no spaces in a variable name allowed.
print( dict.letter a )

-- Correct.
print( dict["letter a"] ) --> prints 1

-- Incorrect. This will look for a key called "key" in the table, not "letter a"
local key = "letter a"
print( dict.key ) --> nil

-- Correct. Now that we're using [], it will evaluate the variable called 'key'
local key = "letter a"
print( dict[key] ) --> 1

Assigning values to keys is no less difficult. We just index the table for the key we want to assign, and set it equal to some value.

local dict = {}

dict["letter a"] = 1
dict["letter b"] = 2
dict["letter c"] = 3

print( dict["letter a"] ) --> 1
print( dict["letter b"] ) --> 2
print( dict["letter c"] ) --> 3

Since you can't use table.remove on dictionaries either, we remove key/value pairs from a dictionary by setting their key index to nil.

local dict = {a = 1, b = 2, c = 3}

print( dict.a ) --> 1

dict.a = nil -- remove 'a'

print(dict.a) --> nil

Hope this helps. If you have any other questions, just let me know.

0
great answer 10/10 User#23252 26 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

here is the difference between using the function table.insert() and "dynamically" setting the values of a table

when setting a value using the function, you need to specify a number index, so doing this

local t = {}
table.insert(t,"name","Arsubia12")

will inevitably cause an error, since the second argument is expected as a number , a solution to this limitation is to use square brackets to set the values, or with property assignment syntax

like this;

square brackets

local t = {}
t.name = "Arsubia12"
print(t.name) -- prints Arsubia12

Output: Arsubia12

property assignment

local t = {}
t["name"] = "Katana"
print(t.name) -- prints Arsubia12
print(t["name"]) -- prints Arsubia12

Output:

Arsubia12

Arsubia12

Answer this question