How to add a table to a table at runtime?

I have  the following:

[lua]
local storyboard = require “storyboard”

local widget = require “widget”

– our persistent area

storyboard.state = {}

storyboard.state.data = {}
[/lua]

But when I run I want to add tables to the storyboard.state.data table so I do this:

[lua]

storyboard.state.data = {“guitars”}

[/lua]

But that adds a string not a table.   I’ve tried

[lua]

storyboard.state.data[#storyboard.state.data+1] = {“guitars”}

[/lua]

but that results in an error:

[lua]

…ds/Strings(Builds)/Strings(default)/Strings/main.lua:79: attempt to get length of field ‘data’ (a nil value)

[/lua]

So how can i add tables to tables (empty or otherwise) at runtime?

Thanks in advance!

Ah.  No I’m a muppet.

I was trashing the data table by assigning it nil earlier.   Sorry!

But still got problems :frowning:

I have strings contained in guitarname, guitarstrings, guitarstringdate 

I want to add a table to storyboard.state.data that will house the strings i.e. details relating to the guitar so I send up with …

storyboard.state.data[1].guitarname

storyboard.state.data[1].guitarstrings

storyboard.state.data[1].guitarstringsdate

etc…

so far I have defined

storyboard.state = {}

storyboard.state.data = {}

So how should I add a table at runtime to the data table that can contain the guitarname, guitarstrings and guitarstrtingsdate

I tried

     storyboard.state.data[#storyboard.state.data+1] = {guitarname, guitarstrings, guitarstringsdate}

and that seems to work, i.e. it appears to be a table but in the Glider variables stack it shows that the type for storyboard.state.data[1] is 

    

java.lang.integer cannot be cast to java.lang.string

Hi dweezil,

There are a bunch of ways to do this depending on how you want to organize your data.  If you want your data to be a regular array, where each item is a dictionary table containing info about a guitar, you could do this:

[lua]

storyboard.state.data[#storyboard.state.data + 1] = {guitarname=guitarname, guitarstrings=guitarstrings, guitarstringsdate=guitarstringsdate}

[/lua]

The right side might look funny, but what it’s doing is creating a dictionary where the key name happens to be the same as the variable name you mentioned you were using.  It’s equivalent to (but shorter than) this:

[lua]

storyboard.state.data[#storyboard.state.data + 1] = {}

storyboard.state.data[#storyboard.state.data].guitarname = guitarname

storyboard.state.data[#storyboard.state.data].guitarstrings = guitarstrings

storyboard.state.data[#storyboard.state.data].guitarstringsdate = guitarstringsdate

[/lua]

Alternatively, if you want data to be a dictionary, where you can use the guitarname as the key to get info about that guitar, you could do this:

[lua]

storyboard.state.data[guitarname] = {guitarstrings=guitarstrings, guitarstringsdate=guitarstringsdate}

[/lua]

Hope this helps.

  • Andrew

Great, ta.   Lua tables are pretty powerful…

Ah.  No I’m a muppet.

I was trashing the data table by assigning it nil earlier.   Sorry!

But still got problems :frowning:

I have strings contained in guitarname, guitarstrings, guitarstringdate 

I want to add a table to storyboard.state.data that will house the strings i.e. details relating to the guitar so I send up with …

storyboard.state.data[1].guitarname

storyboard.state.data[1].guitarstrings

storyboard.state.data[1].guitarstringsdate

etc…

so far I have defined

storyboard.state = {}

storyboard.state.data = {}

So how should I add a table at runtime to the data table that can contain the guitarname, guitarstrings and guitarstrtingsdate

I tried

     storyboard.state.data[#storyboard.state.data+1] = {guitarname, guitarstrings, guitarstringsdate}

and that seems to work, i.e. it appears to be a table but in the Glider variables stack it shows that the type for storyboard.state.data[1] is 

    

java.lang.integer cannot be cast to java.lang.string

Hi dweezil,

There are a bunch of ways to do this depending on how you want to organize your data.  If you want your data to be a regular array, where each item is a dictionary table containing info about a guitar, you could do this:

[lua]

storyboard.state.data[#storyboard.state.data + 1] = {guitarname=guitarname, guitarstrings=guitarstrings, guitarstringsdate=guitarstringsdate}

[/lua]

The right side might look funny, but what it’s doing is creating a dictionary where the key name happens to be the same as the variable name you mentioned you were using.  It’s equivalent to (but shorter than) this:

[lua]

storyboard.state.data[#storyboard.state.data + 1] = {}

storyboard.state.data[#storyboard.state.data].guitarname = guitarname

storyboard.state.data[#storyboard.state.data].guitarstrings = guitarstrings

storyboard.state.data[#storyboard.state.data].guitarstringsdate = guitarstringsdate

[/lua]

Alternatively, if you want data to be a dictionary, where you can use the guitarname as the key to get info about that guitar, you could do this:

[lua]

storyboard.state.data[guitarname] = {guitarstrings=guitarstrings, guitarstringsdate=guitarstringsdate}

[/lua]

Hope this helps.

  • Andrew

Great, ta.   Lua tables are pretty powerful…