lua-users home
lua-l archive

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




On Friday, November 14, 2014, Zulfihar Avuliya <zulfihar.05@hotmail.co.uk> wrote:
Hello,

Here is an example program which I would like to ask :

function change(i)
p={}
table.insert(p, 1, i)
for v in pairs(p) do
print(p[v])
end
end

change(4)
change(3)
change(2)
change(1)
change(0)
print(table.getn(p))
return i


I would like to insert the values of the table p by calling the function change(i),But I am unable to insert the values correctly.  After inserting I need the table to be like :
p[1] = 4
p[2] = 3
p[3] = 2
p[4] = 1
p[5] = 0
Also I need to print the length of the table. Kindly correct my program. Thanks in advance :)


You are reassigning p every time. Try this

p = p or {}