How to get a specific "key" or "index" from a table?

  1. What do you want to achieve?
    I want to get a specific “key” or “index” (location or order) from a table (Dictionary)
  2. 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.
  3. 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.
1 Like

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.

1 Like

I highly appreciate it… I didn’t want to assume it wasn’t possible, as most of the time there is always a way.

1 Like

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.

2 Likes

Roblox now also has their table.find function

That only works on array I believe. It won’t work on dictionary, but it’s a good thing to have.

1 Like

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. :laughing:
Too bad it doesn’t work for dictionaries.

1 Like

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.

Mine works perfectly too. You can turn that into a function like what @cjjdawg did.

1 Like

I’ll test it out. Thank you for the suggestion.

1 Like

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:
image

But this table also prints the same:

Dictionary = {
	["b"] = 3;
	["a"] = 2;
	["c"] = 1;
}

image

Use at your own risk.

3 Likes

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)