How can I search for specific key in a dictionary?

I honestly tried looking everywhere, but none of the answers in the devforum (or outside) were even related…
I’m just looking for a way to look through the keys in a dictionary to find a match using a desired value as input, pretty much like using the find() method for tables.
Surely there must be a more efficient way than just checking if a key == nil… (I honestly havent even tested that yet but I’m really hoping I’m just missing some important info)

1 Like

Sorry wow I did a terrible job at phrasing that, tl;dr I’m just looking for a way to check if a given value is a key in a dictionary and nothing else, just if it exists in there.

1 Like

Yea you did a terrible job explaining that lol but anyways this is how you do it:

for i,v in pairs(Table)
     print(i,v)
do

Thats like all. i being a key. I suppose this is what you want?

That just iterates over the entire dictionary, it feels like the most inefficient solution. I was hoping there’d be a method similar to the table library’s find(), I could just have a key as a parameter and I’ll receive a bool as confirmation if it exists or not

local foo = {  }

if foo[ "key" ] ~= nil then 
       
end 

I don’t see why this wouldn’t work.

1 Like

They’re asking for a “Dictionary Key”, Which I’d assume their format is finding the “Key” of a Value.

local Dictionary = {
["Value"] = "Key"
}

Though, I can’t tell from the format of the Original Post.

1 Like

Ohhh I see.

local Table = {
     Hello = "Hi",
     World = "Earth",
}
print(table.find(Table,Hello)) -- Would print 1
print(table.find(Table,Bye)) -- Would print nil

Is that working for you, as that wouldn’t generally work on its own in the way you’ve inputted it.

Ah I am sorry, I wrote it in forum so I forgot about a mistake. Here:

local Table = {
	Hello = "Hi",
	World = "Earth",
}
print(Table["Hello"]) -- Would print Hi
print(Table["Bye"]) -- Would print nil

My mistake, in this specific form it’ll work.

Yeah turns out this may just be the better solution. I was hopelessly trying to find a more efficient workaround…

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.