Empty Table Manipulation

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Empty Table Manipulation

Kristopher Lohmuller
I am a novice user of Lua so please excuse the lack of knowledge.

I'm trying to perform checks and other functions on an empty table, such
as table.getn(table). This was something I thought would give a result
of zero as per the first edition of Programming in Lua. However, I am in
actuality getting an error for trying to index a nil value. What is
upsetting more is that I've seen another script use this same call and
not generate an error.

To give more clarity on code:

disp = {};
if ((not disp) or (table.getn(disp)==0)) then
   do stuff
end

Further on I try to use the same variable in a for statement:

for i,d in disp do
   do things
end

This also is generating an error about trying to access a nil value.
Again, this is an exact copy of code that works, yet mine does not. Any
suggestions?
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Alex Queiroz
Hallo,

On 4/20/06, Kristopher Lohmuller <[hidden email]> wrote:
>
> This also is generating an error about trying to access a nil value.
> Again, this is an exact copy of code that works, yet mine does not. Any
> suggestions?
>

     Perhaps you're trying this code in Lua 5.1? table.getn and
table.setn are gone.

--
-alex
http://www.ventonegro.org/
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Javier Guerra Giraldez
On Thursday 20 April 2006 3:13 pm, Alex Queiroz wrote:
> Hallo,
>
> On 4/20/06, Kristopher Lohmuller <[hidden email]> wrote:
> > This also is generating an error about trying to access a nil value.
> > Again, this is an exact copy of code that works, yet mine does not. Any
> > suggestions?
>
>      Perhaps you're trying this code in Lua 5.1? table.getn and
> table.setn are gone.

i think the 'table as iterator' idiom is history too.

--
Javier

attachment0 (207 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Luiz Henrique de Figueiredo
> i think the 'table as iterator' idiom is history too.

Yes. Replace "for k,v in t" by "for k,v in pairs(t)" or "for k,v in next,t" .
--lhf
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Kristopher Lohmuller
In reply to this post by Alex Queiroz
The interpreter this goes to is using 5.0 still.
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Kristopher Lohmuller
In reply to this post by Luiz Henrique de Figueiredo
thank you, tried this even though the interpreter is using Lua 5.0, but
it still doesn't like the empty table and still calls it a nil value.
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Kristopher Lohmuller
In reply to this post by Alex Queiroz
okay, I solved my issue with table.getn(disp) it appears. But I am still
having an issue with for i,d in disp (and in using for i,d in
pairs(disp) ) giving me an error for trying to access a nil value.
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Luis Carvalho-2
In reply to this post by Kristopher Lohmuller
> if ((not disp) or (table.getn(disp)==0)) then
>   do stuff
> end

in Lua 5.1 becomes

if not disp or not next(disp) then
  -- do stuff
end

and

> for i,d in disp do
>   do things
> end

becomes

for i,d in pairs(disp) do
  -- do stuff
end

or

for i,d in next, disp do
  -- do stuff
end

Cheers,
luis.

--
A mathematician is a device for turning coffee into theorems.
        -- P. Erdos

--
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Kristopher Lohmuller
Luis Carvalho wrote:

>> if ((not disp) or (table.getn(disp)==0)) then
>>   do stuff
>> end
>
> in Lua 5.1 becomes
>
> if not disp or not next(disp) then
>   -- do stuff
> end
>
> and
>
>> for i,d in disp do
>>   do things
>> end
>
> becomes
>
> for i,d in pairs(disp) do
>   -- do stuff
> end
>
> or
>
> for i,d in next, disp do
>   -- do stuff
> end
>
> Cheers,
> luis.
>

Thanks. For some reason 5.0 accepts the (not disp and not next(disp)),
but it still doesn't like the alternatives in the for statement.
Recognizes that they are valid calls, but still says it is expecting a
table value for disp but that disp is nil. Odd thing is that it gives
this error even if the table has a value.
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Javier Guerra Giraldez
On Thursday 20 April 2006 4:12 pm, Kristopher Lohmuller wrote:
> Thanks. For some reason 5.0 accepts the (not disp and not next(disp)),
> but it still doesn't like the alternatives in the for statement.
> Recognizes that they are valid calls, but still says it is expecting a
> table value for disp but that disp is nil. Odd thing is that it gives
> this error even if the table has a value.

that sounds like you're not giving it the right table.  double check for typos
and scope.  maybe you're setting a local 'disp' and accessing a global 'disp'
or something like that

remember, if you try to access a nonexistant variable, it won't give you any
error, just return a nil value. (and fail somewhere else because of that nil
value)

--
Javier

attachment0 (207 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Kristopher Lohmuller
Javier Guerra wrote:

> On Thursday 20 April 2006 4:12 pm, Kristopher Lohmuller wrote:
>> Thanks. For some reason 5.0 accepts the (not disp and not next(disp)),
>> but it still doesn't like the alternatives in the for statement.
>> Recognizes that they are valid calls, but still says it is expecting a
>> table value for disp but that disp is nil. Odd thing is that it gives
>> this error even if the table has a value.
>
> that sounds like you're not giving it the right table.  double check for typos
> and scope.  maybe you're setting a local 'disp' and accessing a global 'disp'
> or something like that
>
> remember, if you try to access a nonexistant variable, it won't give you any
> error, just return a nil value. (and fail somewhere else because of that nil
> value)
>

What is going on is that disp is a local variable that is assigned the
value of a global variable. The global is a blank table normally, but
values can be saved in it. What makes me scratch my head is that if
there is a value in the global table, I still get the error. What makes
me scratch my head even more is that even with that error, the for
statement still proceeds normally and correctly.
Reply | Threaded
Open this post in threaded view
|

Re: Empty Table Manipulation

Aaron Brown
Kristopher Lohmuller wrote:

> What is going on is that disp is a local variable that is
> assigned the value of a global variable.

See if you can come up with a small but complete (and
ideally, self-contained) example that demonstrates the
problem.  Because, as Javier said, something else is going
on that isn't visible in the examples you gave.

--
Aaron