Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Check table entry is nil...
Started by andersson_d1 May 07 2013 02:46 AM

- - - - -
5 replies to this topic
table check nil add to table

Best Answer thodah11 , 07 May 2013 - 03:20 AM

Hi!

 

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

 

 

 

 

table = {
{startX = 1, startY = 3, hasDot = true}
{startX = 2, startY = 2, hasDot = true}
}
 
--[[
so
 
table[2].hasDot == false
--]]
 

 

This is what I did:

 

 

1. lines = {} -- I have this.
2. 
3. 
4.
5.     if(lines[id] == nil) then
6.     lines[id].startX = grp.parent.x + x1
7.     lines[id].startY = grp.parent.y + height_s - y1
8.     lines[id].hasDot = true

 

Line 6 here is says "attempt to index field '?' (a nil value)"

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

 

 

 

 

I thought this had an obvious answer but I didn't find anything on google:S

 

Regards!

 

 

do something like:

 

if(lines[id] == nil) then
 lines[id]={startX = grp.parent.x + x1 ,startY = grp.parent.y + height_s - y1 ,hasDot = true}
end

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

andersson_d1

[GLOBAL: userInfoPane.html]
andersson_d1
  • Observer

  • 13 posts
  • Corona SDK

Hi!

 

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

 

 

 

 

table = {
{startX = 1, startY = 3, hasDot = true}
{startX = 2, startY = 2, hasDot = true}
}
 
--[[
so
 
table[2].hasDot == false
--]]
 

 

This is what I did:

 

 

1. lines = {} -- I have this.
2. 
3. 
4.
5.     if(lines[id] == nil) then
6.     lines[id].startX = grp.parent.x + x1
7.     lines[id].startY = grp.parent.y + height_s - y1
8.     lines[id].hasDot = true

 

Line 6 here is says "attempt to index field '?' (a nil value)"

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

 

 

 

 

I thought this had an obvious answer but I didn't find anything on google:S

 

Regards!


Edited by andersson_d1, 07 May 2013 - 03:11 AM.


[TOPIC: post.html]
#2

thodah11

[GLOBAL: userInfoPane.html]
thodah11
  • Enthusiast

  • 45 posts
  • Corona SDK

check it with table[i]~=nil should work for example:

 

if(table[1]~=nil)then
 print("initiated")
else
--handle it
end
 


[TOPIC: post.html]
#3

andersson_d1

[GLOBAL: userInfoPane.html]
andersson_d1
  • Observer

  • 13 posts
  • Corona SDK

check it with table[i]~=nil should work for example:

 

if(table[1]~=nil)then
 print("initiated")
else
--handle it
end
 

 

I'm sorry I edited the post while you wrote. But thank you it works better now though the problem now is I cant add stuff to the table so its a table with tables like the example above.



[TOPIC: post.html]
#4

thodah11

[GLOBAL: userInfoPane.html]
thodah11
  • Enthusiast

  • 45 posts
  • Corona SDK

  Best Answer

Hi!

 

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

 

 

 

 

table = {
{startX = 1, startY = 3, hasDot = true}
{startX = 2, startY = 2, hasDot = true}
}
 
--[[
so
 
table[2].hasDot == false
--]]
 

 

This is what I did:

 

 

1. lines = {} -- I have this.
2. 
3. 
4.
5.     if(lines[id] == nil) then
6.     lines[id].startX = grp.parent.x + x1
7.     lines[id].startY = grp.parent.y + height_s - y1
8.     lines[id].hasDot = true

 

Line 6 here is says "attempt to index field '?' (a nil value)"

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

 

 

 

 

I thought this had an obvious answer but I didn't find anything on google:S

 

Regards!

 

 

do something like:

 

if(lines[id] == nil) then
 lines[id]={startX = grp.parent.x + x1 ,startY = grp.parent.y + height_s - y1 ,hasDot = true}
end


[TOPIC: post.html]
#5

andersson_d1

[GLOBAL: userInfoPane.html]
andersson_d1
  • Observer

  • 13 posts
  • Corona SDK

Ah, of course! Thats how you add a table to a table. Thank you!



[TOPIC: post.html]
#6

mpappas

[GLOBAL: userInfoPane.html]
mpappas
  • Contributor

  • 527 posts
  • Enterprise

Hi!

 

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

 

 

 

 

table = {
{startX = 1, startY = 3, hasDot = true}
{startX = 2, startY = 2, hasDot = true}
}
 
--[[
so
 
table[2].hasDot == false
--]]
 

 

This is what I did:

 

 

1. lines = {} -- I have this.
2. 
3. 
4.
5.     if(lines[id] == nil) then
6.     lines[id].startX = grp.parent.x + x1
7.     lines[id].startY = grp.parent.y + height_s - y1
8.     lines[id].hasDot = true

 

Line 6 here is says "attempt to index field '?' (a nil value)"

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

 

 

 

 

I thought this had an obvious answer but I didn't find anything on google:S

 

Regards!

 

Just for clarity -- You couldn't set the fields startX, startY etc because the table element lines[id] did not exist yet (was nil).

 

Adding the statement lines[id] = {} after your nil test (line 5) would allow the rest of the code to work as written.

 

Without it, the .startX .startY references would be trying to access a non-existent table element (which is different than one that exists, but is empty, as created with the lines[id] = {} statement).




[topic_controls]
[/topic_controls]