Deleting non-numerical index from table

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

Deleting non-numerical index from table

Kevin Chen-2
Once I have used a non-numerical table index, is it possible to delete the
index itself so that it is not returned from a call to next()?

For example:

table = {}
table["index"] = true    -- arbitrary value
table["index"] = nil    -- delete the value, so shouldn't the index itself
be non-existent?
i = next(table)    -- programmer expects resulting i to be nil
if(table[i] ~= nil) then
    print("Why am I here?")
end


Reply | Threaded
Open this post in threaded view
|

RE: Deleting non-numerical index from table

Dolan, Ryanne Thomas (UMR-Student)

Which version of Lua are you using?  I ran your script and didn't have any problems under 5.1.

You are correct in that next(table) should return nil;  I don't know why it would behave differently for you unless you are using a broken version of Lua or something.

-----Original Message-----
From: [hidden email] on behalf of Kevin Chen
Sent: Sun 5/7/2006 4:51 PM
To: [hidden email]
Subject: Deleting non-numerical index from table
 
Once I have used a non-numerical table index, is it possible to delete the
index itself so that it is not returned from a call to next()?

For example:

table = {}
table["index"] = true    -- arbitrary value
table["index"] = nil    -- delete the value, so shouldn't the index itself
be non-existent?
i = next(table)    -- programmer expects resulting i to be nil
if(table[i] ~= nil) then
    print("Why am I here?")
end





winmail.dat (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Deleting non-numerical index from table

Luiz Henrique de Figueiredo
In reply to this post by Kevin Chen-2
Did you really get "Why am I here?" as output??
--lhf

> Once I have used a non-numerical table index, is it possible to delete the
> index itself so that it is not returned from a call to next()?
>
> For example:
>
> table = {}
> table["index"] = true    -- arbitrary value
> table["index"] = nil    -- delete the value, so shouldn't the index itself
> be non-existent?
> i = next(table)    -- programmer expects resulting i to be nil
> if(table[i] ~= nil) then
>     print("Why am I here?")
> end
>
Reply | Threaded
Open this post in threaded view
|

Re: Deleting non-numerical index from table

Norman Ramsey-2
 > Did you really get "Why am I here?" as output??

Of course he did --- 'table' is a predefined global with lots of other
entries...  


 > --lhf
 >
 > > Once I have used a non-numerical table index, is it possible to delete
 > > the index itself so that it is not returned from a call to next()?
 > >
 > > For example:
 > >
 > > table = {} table["index"] = true    -- arbitrary value table["index"] =
 > > nil    -- delete the value, so shouldn't the index itself be
 > > non-existent? i = next(table)    -- programmer expects resulting i to be
 > > nil if(table[i] ~= nil) then
 > >     print("Why am I here?")
 > > end
 > >
Reply | Threaded
Open this post in threaded view
|

RE: Deleting non-numerical index from table

Dolan, Ryanne Thomas (UMR-Student)

yes, but he explicitly replaces that table with table = {}, unless the code listed below is different from what he was actually testing, which is very likely.

-----Original Message-----
From: [hidden email] on behalf of Norman Ramsey
Sent: Sun 5/7/2006 8:23 PM
To: Lua list
Subject: Re: Deleting non-numerical index from table
 
 > Did you really get "Why am I here?" as output??

Of course he did --- 'table' is a predefined global with lots of other
entries...  


 > --lhf
 >
 > > Once I have used a non-numerical table index, is it possible to delete
 > > the index itself so that it is not returned from a call to next()?
 > >
 > > For example:
 > >
 > > table = {} table["index"] = true    -- arbitrary value table["index"] =
 > > nil    -- delete the value, so shouldn't the index itself be
 > > non-existent? i = next(table)    -- programmer expects resulting i to be
 > > nil if(table[i] ~= nil) then
 > >     print("Why am I here?")
 > > end
 > >



winmail.dat (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Deleting non-numerical index from table

Norman Ramsey-2
In reply to this post by Norman Ramsey-2
 >  > Did you really get "Why am I here?" as output??
 >
 > Of course he did --- 'table' is a predefined global with lots of other
 > entries...  

Following up my own post, I am blind (having missed the assignment
'table = { }'.