0

I'm a very new to lua so am happy to read material if it will help with tables.

I've decoded a json object and would like to build a table properly using its data, rather than writing 64 lines of the below:

a = {}
  a[decode.var1[1].aId] = {decode.var2[1].bId, decode.var3[1].cId}
  a[decode.var1[2].aId] = {decode.var2[2].bId, decode.var3[2].cId}
  a[decode.var1[3].aId] = {decode.var2[3].bId, decode.var3[3].cId}
  ...etc

Because the numbers are consecutive 1-64, i presume i should be able to build it using a for loop. Unfortunately despite going through table building ideas I cannot seem to find a way to do it, or find anything on creating nested tables using a loop.

Any help or direction would be appreciated.

2 Answers 2

1

Lua for-loops are, at least in my opinion, pretty easy to understand:

for i = 1, 10 do
   print(i)
end

This loop inclusively prints the positive integers 1 through 10.

Lua for-loops also take an optional third argument--which defaults to 1--that indicates the step of the loop:

for i = 1, 10, 2 do
   print(i)
end

This loop prints the numbers 1 through 10 but skips every other number, that is, it has a step of 2; therefore, it will print 1 3 5 7 9.

In the case of your example, if I understand it correctly, it seems that you know the minimum and maximum bounds of your for loops, which are 1 and 64, respectively. You could write a loop to decode the values and put them in a table like so:

local a = {}

for i = 1, 64 do
    a[decodevar.var1[i].aId] = {decode.var2[i].bId, decode.var3[i].cId}
end
Sign up to request clarification or add additional context in comments.

1 Comment

perfect - thanks very much. it looks simple when you put it down. i did try this method but it failed - then i realised that the is possible for there to be less than 64 entires so i need to find a way to count through the JSON. But thank you!
0

What you can do is generating a new table with all the contents from the decoded JSON with a for loop.

For example,

function jsonParse(jsonObj)
    local tbl = {}
    for i = 1, 64 do
        a[decodevar.var1[i].aId] = {decode.var2[i].bId, decode.var3[i].cId}
    end
    return tbl
end

To deal with nested cases, you can recursively call that method as follows

function jsonParse(jsonObj)
    local tbl = {}
    for i = 1, 64 do
        a[decodevar.var1[i].aId] = {decode.var2[i].bId, decode.var3[i].cId}
        if type(decode.var2[i].bId) == "table" then
             a[decodevar.var1[i].aid[0] = jsonParse(decode.var2[i].bId)
        end
    end
end

By the way, I can't understand why are you trying to create a table using a table that have done the job you want already. I assume they are just random and you may have to edit the code with the structure of the decodevar variable you have

1 Comment

Cheers!! the data changes and i need to keep a record of it. so the function to create the tables run several times.

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.