Lua: Functions with Tables for Beginners - Proper Naming/Retrieving of Tables within Tables

StackOverflow https://stackoverflow.com/questions/23573683

  •  19-07-2023
  •  | 
  •  

Question

I am having a horrible time at grasping functions and tables. I've asked a question before that is similar to this but still am having problems getting this to work properly. So I will be more descriptive. But just when I think I understand it I completely confuse myself again. Here is what I am trying to accomplish:

I have a program that is receiving its input from an outside source. It needs to take that input, and basically "dissect" the strings to get the required information. Based on the information it receives, it moves onto the next phase or functions to do the appropriate actions. For example:

input is received as NY345,de,M,9900

I created a table that has all of the different ways the specific input can begin, such as:

local t = {["NY"] = 5, ["MS"] = 7, ["HG"] = 10, ["JX"] = 14, ["UY"] = 20}

Now I want to use a function to receive the input and look for k in t{} and use that to gather other variables...

function seek(input)
    for k, v in pairs (seek) do
        local info = string.match(input,k)
            if info then
                return {seekData = string.match(input,k..",(%d*),.*"), seekMult = seekData*v}
            end
    end
end

How far off am I?

If I had the table "t = {...}" above, and that contained other tables; how can I name each table inside of "t = {...}" and retrieve it for other equations? Such as if ["a"] = 8, the rest of that table was to be utilized? For example:

t={["a"] = 2, ["b"] = 3, ["c"] = "IOS"},{["a"] = 8, ["b"] = 9, ["c"] = "NVY"},{["a"] = 1, ["b"] = 5, ["c"] = "CWQ"}}

if a = 8, then b = 9 and c = "NVY"

I would like my function to search k (of each table) and compare it with the input. If that was found, then it would set the other two local variables to b and c?

Thanks for your help!

Was it helpful?

Solution

I will only answer question 1, as 2 and 3 should be separate questions. There are many ways to do this based on specifics you don't mention but assuming you have a table t like this:

t={
    {["a"] = 2, ["b"] = 3, ["c"] = "IOS"},
    {["a"] = 8, ["b"] = 9, ["c"] = "NVY"},
    {["a"] = 1, ["b"] = 5, ["c"] = "CWQ"}
}

then a function that takes an a key value to look for and returns b and c:

function findItem(a, yourTable)
    for i,tt in ipairs(yourTable) do
         if tt.a == a then
              return i, tt.b, tt.c
         end
    end
end

With this, if the input is k, then

i, b, c = findItem(k, t)
if i == nil then
     print('could not find k')
else
     print('found k at index ' .. i) 
end

The findItem could of course just return the subtable found, and maybe you don't need index:

function findItem(a, yourTable)
    for i,tt in ipairs(yourTable) do
         if tt.a == a then
              return tt
         end
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top