Checking a value against every value in a table
Back with another LUA question:
I am interested if LUA inherently supports checking a value against ANY value in a table. Let me explain. Let us say we have a table with some values: Lua Code:
Is there any way I can check some variable against the whole table as in: Lua Code:
I am curious because using a Lua Code:
Thank you for your time and assistance! |
Your table is an indexed table; every value is automatically assigned an index number. Any kind of 'contains' function will use either a loop or a search algorithm to find the correct value.
If you set it up like this: Code:
local myTable = { Code:
if myTable[someVariable] then ... |
Well, perhaps I should delve a bit deeper into what I am striving for. Let us say that I have an input that may have ANY value. I want to check this against a table of any possible value that may be considered valid. Let me set up a few examples:
local input = "Haleth" local table = { "Phanx", "Clamsoda", "Seerah", "Dridzt", "Haleth" } What I would like to do is construct an IF statement that if the input is ever ANY value that is in said table, that we may proceed with the code. And this process will remain true for any amount of values I add to, or remove from the table, and is independent of the input value. The input may become "apple", or "seven", in which case it wouldn't match, unless "apple" or "seven" was added to the table. Additionally, if I removed "Haleth" from the table the input would no longer be true. I am very sorry if I missed your point, or not illustrating mine correctly. |
Haleth's example is how you do this unless you have some reason not to just use the value you're looking up as the key, like if the values aren't unique, and even then if the table is large enough you might consider constructing a lookup table.
|
Ahh, I do believe that I read Haleth's post too quickly, and this his solution will be perfect. I have been up and at this for....some time, so excuse the mistake.
Thank you much for the input! Edit: Working perfectly, thank you for your input Haleth, and for making me re-read it Semlar. |
I understand that you solved your problem by altering your table from an array style table to a hash style table.
However for others that might stumble on this thread I'll add that Blizz has implemented a global function in UIParent that allows checking for the existence of a value in an array style table. Code:
local exists = tContains(table, value) |
Wouldn't this just be a wrapper for the pairs() loop?
|
The tContains function is implemented like this:
Code:
function tContains(table, item) |
*nod*
But thanks for that, Dridzt. It's useful if you don't want to (or can't) change your table structure and would rather use the wrapper function. |
Quote:
local table = { "Phanx" = 1, "Clamsoda" = 1, "Seerah" = 1, "Dridzt" = 1, "Haleth" = 1 } If you add Phanx to the table, you would do table["Phanx"] = (table["Phanx"] or 0) + 1 Checking if it exists is just if (table["Phanx"]) then ... The key is the string, and the value is the number of times it is added to the table. This method would obviously not work if you need the table to be sorted in a particular order instead. |
Yes, but your syntax is bad, Xin. You must be rusty from all that time away!
This works: Code:
local tbl = { Code:
local tbl = { Code:
local tbl = { |
They were general examples that I hand wrote in the message box. All of my variables have lengthy, well descriptive names.
|
Yes, but your "general examples" get copied and pasted as literal examples of working code by novices who don't know any better. :p
|
Touché, touché. :D
Also, thank you everyone for the input, it truly did help me get to where I wanted to be with my AddOn. |
All times are GMT -6. The time now is 10:44 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI