Table within a table help

So working again with table views. Essentially I am trying to to take data from within a table that is within a table and for now print it out to the console. Once i have it working in the console, I can make it do what i need to do, but I am having issues with this part of it.
 
Here is the table…

 albumData = {       {albumName = "Cry Sister - Single", albumYear = "2013", albumCover = "crysister.png",          songsTitle = {"Cry Sister"},         songsURL = {"audio/crysister-pre.m4a"}         },       {albumName = "Waiting Out The Storm", albumYear = "2012", albumCover = "waitingstorm.png",         songsTitle = {"I Got This", "Monster"},         songsURL = {"audio/igot-pre.m4a", "audio/monst-pre.m4a"}         }      }

 
The tables songsTitle, and songsURL are both tables within the master table albumData (if I have the table built right). Both of those sub tables will have varying data indexes within. Some may have 1 entry, some 3 some 12 etc… 
 
The table view is not my problem, I can pull the first three variables and have them display properly with no issues. The issue is when i try to loop the secondary tables.
 
What I have been doing is this…

for i = 1, #albumData do            print(albumData[i].albumName)     print(albumData[i].albumYear)       print(albumData[i].albumCover) end

 
i have tried creating a new loop within the loop, but it did not seem to work. Basically I need to be able to loop through each album in the table and while doing that loop through each song to make a list that coincides with the album. So when printed to console the above table should look like…

Cry Sister - Single 2013 crysister.png Cry Sister audio/crysister-pre.m4a Waiting Out The Storm 2012 waitingstorm.png I Got This audio/igot-pre.m4a Monster audio/monst-pre.m4a

Any help on this is would be greatly appreciated. Thanks in Advance :slight_smile:

I think this code will work:

 albumData = { {albumName = "Cry Sister - Single", albumYear = "2013", albumCover = "crysister.png", songsTitle = {"Cry Sister"}, songsURL = {"audio/crysister-pre.m4a"} }, {albumName = "Waiting Out The Storm", albumYear = "2012", albumCover = "waitingstorm.png", songsTitle = {"I Got This", "Monster"}, songsURL = {"audio/igot-pre.m4a", "audio/monst-pre.m4a"} } } for i = 1, #albumData do print("Album name: " .. albumData[i].albumName) print("Album year: " .. albumData[i].albumYear) print("Album cover: " .. albumData[i].albumCover) for j = 1, #albumData[i].songsTitle, 1 do print("\t" .. albumData[i].songsTitle[j]) print("\t" .. albumData[i].songsURL[j]) end end

However I would try to include a new table within the album table for songs. This will make it easier in the future if you decide to add a new field that you, for example, receives from a JSON message or retrieves from a database:

 albumData = { {albumName = "Cry Sister - Single", albumYear = "2013", albumCover = "crysister.png", songs = { {songTitle = "Cry Sister", songURL = "audio/crysister-pre.m4a", runtime = "2.54"} }, }, {albumName = "Waiting Out The Storm", albumYear = "2012", albumCover = "waitingstorm.png", songs = { {songTitle = "I got this", songURL = "audio/igot-pre.m4a", runtime = "2.54"}, {songTitle = "Monster", songURL = "audio/monst-pre.m4a", runtime = "2.54"} }, } } print("--------------") for i = 1, #albumData do print("Album name: " .. albumData[i].albumName) print("Release year: " .. albumData[i].albumYear) print("Cover: " .. albumData[i].albumCover) for j = 1, #albumData[i].songs, 1 do print("\t" .. albumData[i].songs[j].songTitle .. " (" .. albumData[i].songs[j].runtime .. ")" .. " - " .. albumData[i].songs[j].songURL) end print("--------------") end

Best regards,

Tomas

Tomas,

Thank you!!! I tried both examples you gave and while both worked, I went with the second one as I can see the this being databased down the road. The first example also seems to use more memory and hangs a brief second when putting the data into the table view. The second example is pretty much instant.

Again, Thanks for coming to my rescue on this!!

Erik

I think this code will work:

 albumData = { {albumName = "Cry Sister - Single", albumYear = "2013", albumCover = "crysister.png", songsTitle = {"Cry Sister"}, songsURL = {"audio/crysister-pre.m4a"} }, {albumName = "Waiting Out The Storm", albumYear = "2012", albumCover = "waitingstorm.png", songsTitle = {"I Got This", "Monster"}, songsURL = {"audio/igot-pre.m4a", "audio/monst-pre.m4a"} } } for i = 1, #albumData do print("Album name: " .. albumData[i].albumName) print("Album year: " .. albumData[i].albumYear) print("Album cover: " .. albumData[i].albumCover) for j = 1, #albumData[i].songsTitle, 1 do print("\t" .. albumData[i].songsTitle[j]) print("\t" .. albumData[i].songsURL[j]) end end

However I would try to include a new table within the album table for songs. This will make it easier in the future if you decide to add a new field that you, for example, receives from a JSON message or retrieves from a database:

 albumData = { {albumName = "Cry Sister - Single", albumYear = "2013", albumCover = "crysister.png", songs = { {songTitle = "Cry Sister", songURL = "audio/crysister-pre.m4a", runtime = "2.54"} }, }, {albumName = "Waiting Out The Storm", albumYear = "2012", albumCover = "waitingstorm.png", songs = { {songTitle = "I got this", songURL = "audio/igot-pre.m4a", runtime = "2.54"}, {songTitle = "Monster", songURL = "audio/monst-pre.m4a", runtime = "2.54"} }, } } print("--------------") for i = 1, #albumData do print("Album name: " .. albumData[i].albumName) print("Release year: " .. albumData[i].albumYear) print("Cover: " .. albumData[i].albumCover) for j = 1, #albumData[i].songs, 1 do print("\t" .. albumData[i].songs[j].songTitle .. " (" .. albumData[i].songs[j].runtime .. ")" .. " - " .. albumData[i].songs[j].songURL) end print("--------------") end

Best regards,

Tomas

Tomas,

Thank you!!! I tried both examples you gave and while both worked, I went with the second one as I can see the this being databased down the road. The first example also seems to use more memory and hangs a brief second when putting the data into the table view. The second example is pretty much instant.

Again, Thanks for coming to my rescue on this!!

Erik