How do I initialize an array where an element in the array is an associative array?

I want to create an associated array for an element used in the definition of properties for a navigation bar. This following works that is not associative:

navBarDefinitions = {
name = “navBar”,
navBarBG = graphics.newGradient(
{ 255, 200, 200 },
{ 200, 70, 70 },
“down” ),
navButtons = { {
navButtonOrder = 1,
navButtonName = NAV_BUTTON_NAME_ADMIN,
navButtonCaption = “Admin”,
navButtonEnabled = true,
navButtonOnRelease = adminProcess
},
{
navButtonOrder = 2,
navButtonName = NAV_BUTTON_NAME_DASHBOARD,
navButtonCaption = “Dashboard”,
navButtonEnabled = true,
navButtonOnRelease = dashboardProcess
}
}
}

What I want to do is make navButtons an associative array where the index is the “constant” string defined in the assignment for navButtonName. (I currently have 7 buttons, but for ease of example, I only included 2)

I then want to be able to access the information as navBarDefninitions.navButtons[NAV_BUTTON_NAME_ADMIN].navButtonOrder, for example.

I have read through so much documentation and tried so many different variations and none have worked so far.

Is this possible? If so then if someone could show me how do to this I would greatly appreciate it! [import]uid: 150008 topic_id: 29585 reply_id: 329585[/import]

Hi there,

Yes, the basic formula for creating an associative array is [lua]table = { key1 = “value1”, key2 = “value2” }[/lua]. With that example, [lua]table[“key1”][/lua] or [lua]table.key1[/lua] would both return the string “value1”.

In your specific case, it seems to me that the following would accomplish what you’re trying to do:
[blockcode]
navBarDefinitions = {
name = “navBar”,
navBarBG = graphics.newGradient(
{ 255, 200, 200 },
{ 200, 70, 70 },
“down” ),

navButtons = {
NAV_BUTTON_NAME_ADMIN = {
navButtonOrder = 1,
navButtonName = NAV_BUTTON_NAME_ADMIN,
navButtonCaption = “Admin”,
navButtonEnabled = true,
navButtonOnRelease = adminProcess
},

NAV_BUTTON_NAME_DASHBOARD = {
navButtonOrder = 2,
navButtonName = NAV_BUTTON_NAME_DASHBOARD,
navButtonCaption = “Dashboard”,
navButtonEnabled = true,
navButtonOnRelease = dashboardProcess
}
}
}
[/blockcode] [import]uid: 109711 topic_id: 29585 reply_id: 118794[/import]

Ok. I had tried that and even though I did not get an error during the declaration, when I tried to reference an element using [lua]print (NAV_BUTTON_NAME_ADMIN…" button order is "…tostring(navBarDefinitions.navButtons[NAV_BUTTON_NAME_ADMIN].navButtonOrder))[/lua]
I got the following error: …attempt to index field ‘?’ (a nil value)…

When I used the following format:
[lua]navBarDefinitions = {
name = “navBar”,
navBarBG = graphics.newGradient(
{ 255, 200, 200 },
{ 200, 70, 70 },
“down” ),
navButtons = { [NAV_BUTTON_NAME_ADMIN] = {
navButtonOrder = 1,
navButtonName = NAV_BUTTON_NAME_ADMIN,
navButtonCaption = “Admin”,
navButtonEnabled = true,
navButtonOnRelease = adminProcess
},
[NAV_BUTTON_NAME_PROPERTY] = {
navButtonOrder = 2,
navButtonName = NAV_BUTTON_NAME_PROPERTY,
navButtonCaption = “Property”,
navButtonEnabled = false,
navButtonOnRelease = propertyProcess
},
{
navButtonOrder = 3,
navButtonName = NAV_BUTTON_NAME_DOCUMENTS,
navButtonCaption = “Documents”,
navButtonEnabled = false,
navButtonOnRelease = documentsProcess
},
{
navButtonOrder = 4,
navButtonName = NAV_BUTTON_NAME_TERMS,
navButtonCaption = “Terms”,
navButtonEnabled = false,
navButtonOnRelease = termsProcess
},
{
navButtonOrder = 5,
navButtonName = NAV_BUTTON_NAME_SIGNATURE,
navButtonCaption = “Signature”,
navButtonEnabled = false,
navButtonOnRelease = signatureProcess
},
{
navButtonOrder = 6,
navButtonName = NAV_BUTTON_NAME_SUBMIT,
navButtonCaption = “Submit”,
navButtonEnabled = false,
navButtonOnRelease = submitProcess
},
{
navButtonOrder = 7,
navButtonName = NAV_BUTTON_NAME_DASHBOARD,
navButtonCaption = “Dashboard”,
navButtonEnabled = true,
navButtonOnRelease = dashboardProcess
}
}
}[/lua]

This latter declaration worked using print statement as shown above, but when I tried to determine the number of buttons in this array using both #navBarDefinitions.navButtons and table.getn(navBarDefinitions.navButtons) the result was 5, not the 7 I expected.

Perhaps my issue is not the declaration but how to determine the number of elements in the associative array. I also tried this format throughout the declaration and I then received a button count of 0. [import]uid: 150008 topic_id: 29585 reply_id: 118799[/import]

Hi there,

You can also use the original format that I suggested, but use the following print statements (notice the quote marks in the first syntax):

[blockcode]
print(navBarDefinitions.navButtons[“NAV_BUTTON_NAME_ADMIN”].navButtonOrder)
print(navBarDefinitions.navButtons.NAV_BUTTON_NAME_ADMIN.navButtonOrder)
[/blockcode]

The output of both print statements is 1.

As for the number of elements, I don’t believe the # operator or [lua]table.getn[/lua] work “out of the box” for an associative array. In your case, the reason you are getting 5 instead of 7 is that your final 5 elements are actually not associative (there is no “key”), so lua is adding them to your table as a regular numerical array. You should have a key for each one, and then I think you have to directly specify the size of the array using [lua]table.setn[/lua] first. See http://www.lua.org/pil/19.1.html for more info.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 29585 reply_id: 118841[/import]

Thanks for all of your help Andrew. I think I have a much better handle on associative arrays. (I’m old school C and Pascal, etc and am used to indexing and records, but not using keys. They’re powerful, and I have get used to the syntax. Clearly not as simple as an index.) Now I have my navigation bar working the way I want and will move on. [import]uid: 150008 topic_id: 29585 reply_id: 118932[/import]

No problem, glad it’s working and that I could be of help!

  • Andrew [import]uid: 109711 topic_id: 29585 reply_id: 118974[/import]