lua-users home
lua-l archive

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


On Thu, Sep 28, 2006 at 05:56:03PM -0700, Wesley Smith wrote:
> I'm trying to "clear" an array after a loop is run and reuse it again
> on the next iteration.  By "clear" I mean that I wat #array to be 0.
> 
> So, if I have:
> 
> array = {}
> 
> --add stuff to array in loop
> array[#array+1] = stuff
> 
> --end loop
> 
> --clear array
> array[1] = nil
> print(#array)
> 
> What I'm getting from the print statement is the length of the array
> after the loop ended, not the expected 0.  Is this correct?  I don't
> see why this wouldn't work.

"The length of a table t is defined to be any integer index n such that
t[n] is not nil and t[n+1] is nil".

You need to clear all of the array indexes.  Most of the time, you can
just do "array = {}", unless you need other references to the same table
cleared, too.

-- 
Glenn Maynard