Passing a key string to a function to access the dictionary value

Hey guys!

I am currently trying to write a guitar chord display app for a bit of practice and I need a bit of help with something. I am trying to call a function to point to a specific table value but corona is saying I am attempting to index field ‘name’ (a nil value) even though I am passing a string to the function. 

Here is my code:

[lua]

– Chord DECLARATIONS

– {refFret, string1, string2, string3, string4, string5, string6}

local chords = {

                    – Test cases

                    open = {{1, 0, 0, 0, 0, 0, 0}},

                    all1 = {{1, 1, 1, 1, 1, 1, 1}},

                    – Chords

                    E =  {{1, 0, 2, 2, 1, 0, 0} },

                    A =  {{1, -1, 0, 2, 2, 2, 0} },

                    D =  {{1, -1, -1, 0, 2, 3, 2} },

                    G =  {{1, 3, 2, -1, -1, -1, 3} },

                    C =  {{1, -1, 3, 2, -1, 1, -1}, {3, 1, 1, 3, 3, 3, 1, 1}},

                    F =  {{1, 1, 3, 3, 2, 1, 1} }

               }

– Called chordDisplay(‘open’)

local function chordDisplay(name)

     – The following does not work

     chordFingering(chords.name[1])

     – does work but don’t want heaps of if/elseif

     if name == ‘open’ then

          chordFingering(chords.open[1])

          --chordSelect.label = chordName

     end

end

[/lua]

Hopefully that makes my problem clearer. I essentially want to be able to call chordDisplay(‘A’) and then the function will call the ‘chordFingering’ function on the key A[1] within the chords dictionary. 

Any help would be greatly appreciated! Thanks guys

I figured it out! I guess to use the ‘name’ argument we need to put it in [square brackets] inside the function. Here is my function that now works:

[lua] 

local function chordDisplay(name, voicing)

     

     chordFingering(chords[name][voicing])

end

[/lua]

I figured it out! I guess to use the ‘name’ argument we need to put it in [square brackets] inside the function. Here is my function that now works:

[lua] 

local function chordDisplay(name, voicing)

     

     chordFingering(chords[name][voicing])

end

[/lua]