lua-users home
lua-l archive

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


Admittedly, I have adopted a lazy and somewhat (perhaps) unconventional approach to learning Lua, i.e. by trying out what I've learnt so far supplanted by a bit of intuition.

Here's one snippet that stumped me. Lua didn't like me doing arithmetic on table fields, but from what I've read so far, I couldn't figure out an obvious reason.

[code]
t = {}
t.distance = 40
t.time = 2.5

for k,v in pairs(t) do print(k,v) end

function speed(tab) return tab.distance/tab.time end

t.speed = speed{t}

for k,v in pairs(t) do print(k,v) end
[/code]

Lua barfs out this output:-

[output]
time    2.5
distance    40
lua: 8.lua:7: attempt to perform arithmetic on field 'distance' (a nil value)
stack traceback:
    8.lua:7: in function 'speed'
    8.lua:9: in main chunk
    [C]: ?
[/output]

Why so ?