How would I insert a string value into a table and call the variable the name of the string value? Something like this, but functional:
{stringValue.Name = stringValue.Value}
How would I insert a string value into a table and call the variable the name of the string value? Something like this, but functional:
{stringValue.Name = stringValue.Value}
If you enclose the key in square brackets, it will evaluate the contents and use that as a key, as opposed to just treating it as a string literal.
{[stringvalue.Name] = stringValue.Value}
This prints as nil
local t = {[script.Strength.Name] = script.Strength.Value}
for Position,a in next,t do
print(a.Strength)
end
When you iterate through a table, you get a key,value pair in each iteration. In this case, youâve defined the keys as Position
and the values as a
. For the first iteration, your for loop will look like this:
for Position=[script.Strength.Name],a=[script.Strength.Value] in next,t do
a.Strength is nil because a
is script.String.Value
â not t
. a
, a string, doesnât have a property/child named Strength
. If you want to access the value by key, you would index the table rather than the value. i.e. t.Strength
or t["Strength"]
Oh, one more question. How would I insert it into the table? This doesnât work
table.insert(t,[script.Strength.Name] = script.Strength.Value)
t[script.Strength.Name] = script.Strength.Value
Tables have two components: an array, indices of integers [1, inf), and a hash, indices of integers (-inf, 0] or values of any other type (decimals, BrickColors, strings, or even tables). Table.insert is used to insert values into the array portion of the table. Itâs a function, so thereâs no equals sign â itâs just:
--inserts at end of array
table.insert(t, value)
or
--inserts value at position and pushes existing values to the end of the array
table.insert(t, positiveIntegerPosition, value)
What youâre using is not an array though â youâre using the hash. To set a value in the hash, you use what @Usering posted: t[keyName] = value or t.keyName = value i.e.
t["Test"] = value
t.Test = value
You can use the square brackets whenever you want, but omitting them can only be done if the key name (Test in this case) is a string and has no special characters.
Actually it can be a negative integer,
Table = {}
Table[-5] = "negFive"
table.insert(Table, -3, ânegThreeâ)
for i, value in pairs(Table) do
print(i, value)
end-5 negFive
-3 negThree
pairs
includes the hash as well. ipairs
only iterates over the array, which doesnât include any indices <= 0.
t = {}
t[4] = 4
t[3] = 3
t[2] = 2
t[1] = 1
t[0] = 0
t[-1] = -1
t[-2] = -2
t["Test"] = "Test"
for _,v in pairs(t) do print(v) end
print("-----------------")
for _,v in ipairs(t) do print(v) end
Result:
That is such bad practice it is even removed in Lua 5.3
> table.insert(t,-1,"test")
stdin:1: bad argument #2 to 'insert' (position out of bounds)
stack traceback:
[C]: in function 'table.insert'
stdin:1: in main chunk
[C]: in ?
>
I guess I was using an outdated version. Not that I use negative indexes, but it worked in my Lua command line ÂŻ\_(ă)_/ÂŻ
Most if not all available pre-builds come with Lua 5.1 for reasons I am not sure of; perhaps there is little interest for up-to-date pre builds with purists not being satisfied without customizing the source anyway. 5.3 has a few neat features but whether building it from scratch is worth that is up to you.