Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I see if a value is equal to something in a table? [Edit: I FIXED IT!]

I would like to have

local table = {"a", "b", "c"} -- etc.
local label = script.Parent.TextLabel.Text
--[[ what I would like to do next is take label and if label is equal to something in the table, do something (I know what and how I will do this next) --]]

I also do not want to use for i, ws in ipairs(table) do because that cycles through every situation and if it gets the other situations and the situation you want it basically skips over it.

Update: I figured out I could still use for

local table = {"a", "b", "c"}
local label = script.Parent.TextLabel.Text
local val = 0

for i, ws in ipairs(table) do
if label == ws then
val = 1
end
end
if val == 1 then
print("D:")
elseif val == 0 then
print(":D")
end

1 answer

✓ Accepted answer

Dictionaries

If your goal is to avoid iteration to see if a value exists in a table, creating your table in dictionary format is something you'll probably want to consider.


"What's dictionary format?"

I've gone over this countless times, so I'll spare the lecture. Creating a table in dictionary format is pretty much having a data structure in which all of it's keys are NOT numeric. This is opposed to an array, in which ALL of it's keys are numeric.


Example

Utilizing this dictionary format would be quite simple. All we'd have to do is basically make our key the value, and associate it with a true or existing value. Here's an example:

local Table = {
	a = true,
	b = true,
	c = true
}

local CompareText = "a" -- The value you want to check the table for

-- The comparison
-- We simply use brackets to indicate looking for a value in our table's index
if Table[CompareText] then
	print("The value exists in the table")
end

And the code above should print "The value exists in the table", since we've indexed it for "a", which does indeed correspond to a true or existing value.


And that's about it. If you have any questions, just let me know.

1 comment

0

I will accept this as well because it was more what I was looking for. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.