1

I'm a PHP guy so I don't know how to solve this problem. I know how I'd do this in PHP but I have no clue what the constraints are for Lua regarding this problem.

T = {
  clocktable = {},
  beancabinet = {},
  --...etc
}

T.clocktable[674] = 1
T.clocktable[660] = 1
--...etc

Q: How would I loop through "T" to quickly know that the clocktable key includes the extended keys "674" and "660", only knowing "clocktable"?

Note: please be careful of overhead as the "T" table will be very loaded with data and this is in a performance environment.

3 Answers 3

6

I'm not sure what the question exactly is. If you know the key, ponzao's answer is right, otherwise use a for loop:

for key, value in pairs(T.clocktable) do
    -- do something with key and value
end
Sign up to request clarification or add additional context in comments.

4 Comments

We wouldn't know the key (674, for example) of T.clocktable. Also, we will need to do actions on each key.
@Geekster: The for k,v in pairs(T) syntax that Robin provided is how you loop through a table, you don't need to know 674 in advance.
Don't you mean T.clocktable[674] (without the second dot?)
@egarcia: I did, an unfortunate typo. Luckily Stuart P. Bentley fixed it for me.
1

Is there a reason not to just check if it is not nil?

T.clocktable[674] ~= nil

1 Comment

We don't know "clocktable[674]" we know "clocktable" at the time of inquiry. The idea is to compile a list of keys so we can use the keys to perform actions.
0

Try

for i, v in pairs(T.clocktable) do
    print("Key:", i, "Value:", v)
end

> Key: 674 Value: 1
> Key: 660 Value: 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.