|
On 2014-11-14 9:09 AM, "Zulfihar Avuliya" <zulfihar.05@hotmail.co.uk> wrote:
>
> function change(i)
> p={}
Your problem is here. Every time change() is called, it's assigning a new, empty table to the global variable p. You need to move that assignment outside the function if you want to use the same table every time.
> Also I need to print the length of the table. Kindly correct my program. Thanks in advance :)
Length of a table is fairly simple, but has a few gotchas.
In Lua 5.1 and later (unsure about 5.0), use #, the length operator:
print("length is", #p)
This works as long as p is a sequence. In other words, if p has integer keys from 1 to n, then #p = n. However, if there are gaps (eg: p={'a', 'b', nil, 'd'}) then you can no longer rely on #p and must keep track of "length" (however you define it) yourself.
Mind that non-integer keys don't matter here, eg the following table is still a sequence:
p = {'a', 'b', 'c', hello ="world"}
p[0.5] = '?'