lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


argh... indeed. i mean, i understand the futility of repeating keys, but shouldn't the repeated key, result in overwrite of the value field, with the new 2nd-dimension table, and thus the print() should not yield nil ?


On Sat, Sep 28, 2013 at 12:36 PM, Choonster TheMage <choonster.2010@gmail.com> wrote:
On 28 September 2013 16:38, Jayanth Acharya <jayachar88@gmail.com> wrote:
> Having a slow day... I was trying to understanding the FSM implementation
> described here: http://lua-users.org/wiki/FiniteStateMachine
>
> And thought of trying this alternative way, to see if I could use the 2-d
> array
>
> snippet:
> -- current-state -> { event --> { new-state, action-function }}
>
> fsm = { s1 = { e1 = { 's2', a1}},
>         s2 = { e2 = { 's3', a2}},
>         s1 = { e2 = { 's1', nil}},
>         s2 = { e3 = { 's4', a3}}
>       }
>
> print(fsm['s1'])
> print(fsm['s1']['e1'])
>
> gives the output:
>>lua -e "io.stdout:setvbuf 'no'" "2darr.lua"
> table: 003EB3D8
> nil
>>Exit code: 0
>
> I was wondering, why the second print() results in nil ? I was expecting a
> table, that contains {'s2', a1} !

You're using s1 and s2 as keys twice in the same table. The second
occurrence of each key in the table literal overwrites the first.