|
| 03 Feb 2015 06:31 PM |
If I have a dictionary:
container = { ["Question"] ={ ["Question"] = "How are you?", ["Yes"] = "Good indeed, sir", ["No"] = "Quite bad, sir", }, ["Question"] ={ ["Question"] = "How is your mother?", ["Yes"] = "Quite good, sir", ["No"] = "Very fat, sir", }, ["Question"] ={ ["Question"] = "Is this working?", ["Yes"] = "Yes indeed, sir", ["No"] = "Unwantingly no, sir", } }
How can I obtain only the 'Question' arrays. Not the double nested ones but the singular ones using indexes?
For example, How can I access the first Array using indexes such as:
counter = 1; repeat print(container[counter].Question); counter = counter + 1; until counter == 3
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Feb 2015 06:36 PM |
You can't, you're just replacing it when you define it multiple timesl ike that. You'd have to do something like:
container = { {Question = whatever, yes = blah, no = blah}; {Question = whatever, yes = blah, no = blah}; };
Then use your bottom method to do it. |
|
|
| Report Abuse |
|
|
LucasLua
|
  |
| Joined: 18 Jun 2008 |
| Total Posts: 7386 |
|
|
| 03 Feb 2015 06:39 PM |
Create your own iterator using a function that mimics next, or make a better organized table. You could put your question tables in their own table.
You can also check the index of a table with the pairs operator.
for index, var in pairs(tab) do if index == "Question" then -- do stuff end end |
|
|
| Report Abuse |
|
|
LucasLua
|
  |
| Joined: 18 Jun 2008 |
| Total Posts: 7386 |
|
|
| 03 Feb 2015 06:40 PM |
| Also cnt makes a good point that you actually are overwriting it which I didn't realise in my post so do what he said. |
|
|
| Report Abuse |
|
|
|
| 03 Feb 2015 06:41 PM |
Ah, Alright. Thanks that solved the issue. |
|
|
| Report Abuse |
|
|