Lua Table Randomization Help

I’m doing something wrong in the code below, but not quite seeing why.

local data = {}  
  
for i = 1, 22 do  
 table.insert( data, data[i].title = "A title here")  
end  
  

I’m simply playing with table views, and want to practice with random numbers of rows.
So instead of setting up the table data manually:

local data = {}  
--setup each row as a new table, then add title, subtitle, and image  
data[1] = {}  
data[1].title = "Hot Coffee"  
data[1].subtitle = "Grounds brewed in hot water"  
data[1].image = "coffee1.png"  
data[1].rID = math.random()  
  
data[2] = {}  
data[2].title = "Iced Coffee"  
data[2].subtitle = "Chilled coffee with ice"  
data[2].image = "coffee2.png"  
data[2].rID = math.random()  

I’d like to create it from a loop. (even if the title, subtitle, and image are all the same)

The syntax error I’m getting back is:
Syntax error: c:\users…\main.lua:13: ‘>’ expected near '='

Can anyone see what I’m doing wrong in that first block of code? [import]uid: 154122 topic_id: 31258 reply_id: 331258[/import]

Can you please post exactly what is Line 13 in your code, and the appropriate lines before and after it? Obviously when you post code blocks here, it doesn’t reflect the line numbers in you Lua code, so it’s hard to narrow it down.

Thanks!
Brent [import]uid: 9747 topic_id: 31258 reply_id: 125014[/import]

Hi there,

I think the problem is that your [lua]table.insert[/lua] statement does not follow the right syntax. See this site: http://lua-users.org/wiki/TablesTutorial. Your second argument is an assignment expression, which doesnt belong there. Instead, you should provide three separate arguments: the table, the position, and the value.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31258 reply_id: 125015[/import]

I just figured it out:
Correct syntax below:

local data = {} for i = 1, 22 do local newdata = { title = "A title here" } table.insert(data, newdata) end [import]uid: 154122 topic_id: 31258 reply_id: 125018[/import]

Can you please post exactly what is Line 13 in your code, and the appropriate lines before and after it? Obviously when you post code blocks here, it doesn’t reflect the line numbers in you Lua code, so it’s hard to narrow it down.

Thanks!
Brent [import]uid: 9747 topic_id: 31258 reply_id: 125014[/import]

Hi there,

I think the problem is that your [lua]table.insert[/lua] statement does not follow the right syntax. See this site: http://lua-users.org/wiki/TablesTutorial. Your second argument is an assignment expression, which doesnt belong there. Instead, you should provide three separate arguments: the table, the position, and the value.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31258 reply_id: 125015[/import]

I just figured it out:
Correct syntax below:

local data = {} for i = 1, 22 do local newdata = { title = "A title here" } table.insert(data, newdata) end [import]uid: 154122 topic_id: 31258 reply_id: 125018[/import]