[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.2 Length Operator and tables (bug?)
- From: Coda Highland <chighland@...>
- Date: Tue, 17 Apr 2012 13:51:02 -0500
On Tue, Apr 17, 2012 at 12:43 PM, Joseph Manning <manning@cs.ucc.ie> wrote:
> On 2012-Apr-17 (Tue) at 11:18 (-0500), Coda Highland wrote:
>
>>> function table.issequence(t)
>>> for i = 1, #t do
>>> if t[i] == nil then return false end
>>> end
>>> return true
>>> end
>
> Counterexample:
>
> $ cat test.lua
> function issequence(t)
> for i = 1, #t do
> if t[i] == nil then return false end
> end
> return true
> end
>
> t = { 1, 2 }
> t[ 2 ] = nil
> t[ 3 ] = 3
>
> print( t[ 1 ], t[ 2 ], t[ 3 ], issequence( t ) )
>
> $ lua -v test.lua
> Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> 1 nil 3 true
>
> Joseph
That's not a counterexample, that's just a special case. In this case,
this test is reporting that t IS a sequence, and #t is returning the
length of it -- what it isn't reporting is that there are also
non-sequence parts of t. If you want THAT test, of course, you can
code for it too.
/s/ Adam