looking for some advice on for loops and printing a dictionary to look like a "spreadsheet"

I’m asking this here in the Lua section because of the current way I’m working on this, but I need to print out a large body of text- in all honesty, about 100 lines easily.  I’m creating a reference chart with names and values.  I plan on it going off the page vertically so that the user can scroll up and down the chart and find the information they’re looking for.

Right now, this is my code for the dictionary itself:

local dict = {} dict.aluminum = {} dict.aluminum[1] = "Aluminum" dict.aluminum[2] = {"Foil",".02",".98","1"} dict.aluminum[3] = {"Polished",".02",".98","2"}

Well, since that post deleted everything I wrote after that first code box…

The current plan is to created several vertical text boxes that hold all of the same kind of information on them: the “dict.<name>[1]” will have a column on the left side, just to the right of it will be a column with the types like “foil” and “polished”, then the next 3 values in three columns.  I’d like to read the appropriate entries from each section read and then separated by new-line chars ("\n") to give the vertical spacing.  I’m going to add the text colums (text objects) together in a display group and then change the y-value of the group as the user moves the “spreadsheet” up and down.

My first task has been to get the reading of the dictionary right (starting with just printing everything I can to the console).  I’m currently stuck at this:

for i = 1, #dict.aluminum[2] do print(dict.aluminum[2][tostring(i)]) end

Which doesn’t work, but doesn’t break either.  I just get “nil” for each of the values (but it does print out the correct number of “nil”'s though, so I have that going for me).

My ultimate question to anyone who has experience doing anything like this is, is there a more effective way of doing this?  I’ve thought about using a JSON file but in this situation, I wouldn’t have any advantage. This is a static text document.  The only way it’s going to be adjusted is by myself and any other programmers in the future adding to the dictionary itself.  I don’t want to “cheat” and list the table coords for each value because it’s not efficient coding.  Why bother making the table in the first place if I was going to do that when I could just manually assign the text when I make the object?  (Please don’t tell me that IS the efficient way of doing this…)

If I’m on the right track, what am I doing wrong?  I’ve been trying to figure out how to use the for loops to read from the table but I’m getting lost since I’m nesting so many tables…

I don’t expect you guys to write my project for me, but if nothing else, I’d like some advice on #1 even if you ignore my #2.

Thanks guys.

the reason it’s printing nil is tostring(), as a key of “1” (a string) is different than a key of 1 (a number), so:

for i = 1, #dict.aluminum[2] do &nbsp; print(dict.aluminum[2][i]) end

i don’t really follow all the details of your “formatting” issue, so can’t offer much on that.

i’d be tempted to suggest some structural changes to the table, mainly using named keys instead of numbers, just to maintain your sanity, but since i don’t really understand what all the values mean and variations in which they might occur, i probably wouldn’t get very far on that either.

The snippet I posted looks like it’d cause insanity quite quickly but they’re 2 reference values for real world surfaces and the source number (like a book citation kind of source) and in the text document I’m copying these from they’re labelled lol.  Let me try out the altered code and I’ll post back in a minute.

and it’s good!  please don’t ask me why I hadn’t tried that yet.  maybe just too many bits of info and too many combinations and it got left out or i didn’t have the dict set up correctly earlier.  bleh.  thanks dave.

i can probably get things working now (if not i’ll add to this thread later) but i’d still appreciate some feedback on the first issue: is there a better way to do this?

Well, since that post deleted everything I wrote after that first code box…

The current plan is to created several vertical text boxes that hold all of the same kind of information on them: the “dict.<name>[1]” will have a column on the left side, just to the right of it will be a column with the types like “foil” and “polished”, then the next 3 values in three columns.  I’d like to read the appropriate entries from each section read and then separated by new-line chars ("\n") to give the vertical spacing.  I’m going to add the text colums (text objects) together in a display group and then change the y-value of the group as the user moves the “spreadsheet” up and down.

My first task has been to get the reading of the dictionary right (starting with just printing everything I can to the console).  I’m currently stuck at this:

for i = 1, #dict.aluminum[2] do print(dict.aluminum[2][tostring(i)]) end

Which doesn’t work, but doesn’t break either.  I just get “nil” for each of the values (but it does print out the correct number of “nil”'s though, so I have that going for me).

My ultimate question to anyone who has experience doing anything like this is, is there a more effective way of doing this?  I’ve thought about using a JSON file but in this situation, I wouldn’t have any advantage. This is a static text document.  The only way it’s going to be adjusted is by myself and any other programmers in the future adding to the dictionary itself.  I don’t want to “cheat” and list the table coords for each value because it’s not efficient coding.  Why bother making the table in the first place if I was going to do that when I could just manually assign the text when I make the object?  (Please don’t tell me that IS the efficient way of doing this…)

If I’m on the right track, what am I doing wrong?  I’ve been trying to figure out how to use the for loops to read from the table but I’m getting lost since I’m nesting so many tables…

I don’t expect you guys to write my project for me, but if nothing else, I’d like some advice on #1 even if you ignore my #2.

Thanks guys.

the reason it’s printing nil is tostring(), as a key of “1” (a string) is different than a key of 1 (a number), so:

for i = 1, #dict.aluminum[2] do &nbsp; print(dict.aluminum[2][i]) end

i don’t really follow all the details of your “formatting” issue, so can’t offer much on that.

i’d be tempted to suggest some structural changes to the table, mainly using named keys instead of numbers, just to maintain your sanity, but since i don’t really understand what all the values mean and variations in which they might occur, i probably wouldn’t get very far on that either.

The snippet I posted looks like it’d cause insanity quite quickly but they’re 2 reference values for real world surfaces and the source number (like a book citation kind of source) and in the text document I’m copying these from they’re labelled lol.  Let me try out the altered code and I’ll post back in a minute.

and it’s good!  please don’t ask me why I hadn’t tried that yet.  maybe just too many bits of info and too many combinations and it got left out or i didn’t have the dict set up correctly earlier.  bleh.  thanks dave.

i can probably get things working now (if not i’ll add to this thread later) but i’d still appreciate some feedback on the first issue: is there a better way to do this?