-
What do you want to achieve?
I want to get a specific “key” or “index” (location or order) from a table (Dictionary) -
What is the issue?
As most of us (hopefully) know, there are 2 parts of a dictionary, the index (commonly referred to as “key”) and the value as shown in screenshots below.
’
My situation regards referring to specific “keys” in the table. (Shown below)
I have done much research but all of my research was to no avail. -
What solutions have you tried so far?
I’ve looked with several searches on the developer forum ranging from “How to get specific key from table” to “Understanding Dictionaries”. The “Understanding Dictionaries” search and typing my title in the draft is where I came closest…
I already understand the basics of the key, value notion. I even looked how to do it outside of LUA…
even they could not present clear answers. Anything helps.
For dictionary, there is no order nor you can tell the location… The key is the name, and the value is its value.
As for arrays, their order is an ascending order. Even though they are like this:
local array = {"a","b","c"}
If you do loop through it, you will get this:
for i,v in ipairs(array) do
print(i,v)
end
-- 1 a
-- 2 b
-- 3 c
In which i
represents the position of the value in the array, and the v
is the value.
I highly appreciate it… I didn’t want to assume it wasn’t possible, as most of the time there is always a way.
To reference from a dictionary you know that you would directly reference it like:
print(fruit.kiwi)
So you could do this:
local m = {
['coeuvho'] = 'jk',
['dwdwdw'] = 'yes'
}
for i, v in pairs (m) do
if v == 'jk' then
print(i) end end
For example here this would only print coeuvho but not dwdwdw.
That only works on array I believe. It won’t work on dictionary, but it’s a good thing to have.
I can go and test it real fast. but I thought it worked for me before when I was using it with dictionaries.
He’s trying to do it in a dictionary. I just tested it and this prints nil if you were to find the index in a dictionary.
Yeah, I just tested it too. But hey it’s a good thing if you want to find it in a table.
Too bad it doesn’t work for dictionaries.
That’s exactly what I was looking for.
if you really want to acess the key from a value then just make a function that searches the dictionary and then returns the key like this:
local function getKeyFromDictVal(Dictionary, SearchValue)
for Key, Value in pairs(Dictionary) do
if (Value == SearchValue) then
return Key
end
end
end
Let me rewrite the code so that it functions flawlessly, then I’ll repost.
I’ll test it out. Thank you for the suggestion.
Consider it a hacky method but it works to an extent.
function GetPositionFromIndex(Table,Index)
local num = 0
for i,_ in pairs(Table) do
num = num+1
if i == Index then
return num
end
end
return nil
end
Dictionary = {
["b"] = 3;
["c"] = 2;
["a"] = 1;
}
for i,v in pairs(Dictionary) do
print(i,GetPositionFromIndex(Dictionary,i),v)
end
The issue with dictionaries is that when run through an in pairs
loop, they print in a random order, not in order from top to bottom down the table.
To make this function properly you’d need to strictly order your dictionaries so that they fit to the order.
i.e: original dictionary prints:
But this table also prints the same:
Dictionary = {
["b"] = 3;
["a"] = 2;
["c"] = 1;
}
Use at your own risk.
I’m almost 100% sure this is what I was looking for. Thank you terry!
If you want them to print in order this is what you can do
local Table = {
{name = "a"};
{name = "b"};
{name = "c"};
}
This will loop through from top to bottom if you use a for loop.
I’ve never seen this formatting. Does this come with guaranteed results?
It should yes, my guess is you could test by typing;
print(Table[1].name)