[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: 2-dimension array initialization / allocation
- From: Choonster TheMage <choonster.2010@...>
- Date: Sat, 28 Sep 2013 17:06:55 +1000
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.