Inserting Nil Into Table?

Can someone explain why this code does not work for me, and also how to fix it:

local a, b, c, d, e  = "one", "two", "three", "four", "five"; local f, g, h; --Nil values local i, j, k = "six", "seven", "eight" local l, m local t = {a,b,c,d,e,f,g,h,i,j,k,l,m} local nilCounter = 0 print("Size of table: " .. #t); for i = 1, #t do     if (t[i] == nil) then nilCounter = nilCounter + 1 end end print("Number of nil in the table: " .. nilCounter);

Expected Output:

Size of table: 13

Number of nil in the table: 5

Actual Output:

Size of table: 5

Number of nil in the table: 0

For some reason, lua doesn’t seem to recognize the nil values in the table, how would I get it to recognize nil values?

Lua tables cannot store nil values.

Lua uses nil to remove table entries instead.  That is, assigning nil to a table entry will remove that entry from the table.

Have a look at the official Lua documentation for this via the link below.

   http://www.lua.org/pil/2.5.html

Darn. I was trying to do something like this:

local t = {a,b,c,d,..etc} for i = 1, #t do     if (t[i] == nil) then nilCounter = nilCounter + 1 end end if (nilCounter == 0) then     executeCode(); end

Instead of this:

if (a == nil or b == nil or c == nil or d == nil or e == nil or f == nil or g == nil or h == nil or i ==nil or j == nil or k == nil ...etc) then       executeCode() end

So am I forced to use version b (which may become tedious for long lists) or is it any way possible to achieve version a?

Here’s an idea.  How about you create your own “null” type in Lua like this…

   local Null = {}

So, instead of assigning nil to your table entries, you can assign it that “Null” variable and check that instead.

local Null local a, b, c, d, e = "one", "two", "three", "four", "five"; local f, g, h = Null, Null, Null local i, j, k = "six", "seven", "eight" local l, m = Null, Null local t = {a,b,c,d,e,f,g,h,i,j,k,l,m} local nullCounter = 0 print("Size of table: " .. #t); for i = 1, #t do if (t[i] == Null) then nullCounter = nullCounter + 1 end end print("Number of nulls in the table: " .. nullCounter);

I cant believe I didn’t think of that. Always learning. Thanks!

Happy to help.

Yeah, the only other alternative that I could think of would be to “box” your values in a table, like how you would box primitve types in Java or values types in C#/VB.NET.  Something like this…

local a = { value = "One" } local b = { value = "Two" } local c = {} -- This does not provide a value (ie: stores nil) local myTable = { a, b, c } for index = 1, #myTable then if myTable[index].value == nil then -- Found a nil value. end end

The above is a bit more work and might be a breaking change for you, but if it proves useful, then have at it.  :slight_smile:

Lua tables cannot store nil values.

Lua uses nil to remove table entries instead.  That is, assigning nil to a table entry will remove that entry from the table.

Have a look at the official Lua documentation for this via the link below.

   http://www.lua.org/pil/2.5.html

Darn. I was trying to do something like this:

local t = {a,b,c,d,..etc} for i = 1, #t do     if (t[i] == nil) then nilCounter = nilCounter + 1 end end if (nilCounter == 0) then     executeCode(); end

Instead of this:

if (a == nil or b == nil or c == nil or d == nil or e == nil or f == nil or g == nil or h == nil or i ==nil or j == nil or k == nil ...etc) then       executeCode() end

So am I forced to use version b (which may become tedious for long lists) or is it any way possible to achieve version a?

Here’s an idea.  How about you create your own “null” type in Lua like this…

   local Null = {}

So, instead of assigning nil to your table entries, you can assign it that “Null” variable and check that instead.

local Null local a, b, c, d, e = "one", "two", "three", "four", "five"; local f, g, h = Null, Null, Null local i, j, k = "six", "seven", "eight" local l, m = Null, Null local t = {a,b,c,d,e,f,g,h,i,j,k,l,m} local nullCounter = 0 print("Size of table: " .. #t); for i = 1, #t do if (t[i] == Null) then nullCounter = nullCounter + 1 end end print("Number of nulls in the table: " .. nullCounter);

I cant believe I didn’t think of that. Always learning. Thanks!

Happy to help.

Yeah, the only other alternative that I could think of would be to “box” your values in a table, like how you would box primitve types in Java or values types in C#/VB.NET.  Something like this…

local a = { value = "One" } local b = { value = "Two" } local c = {} -- This does not provide a value (ie: stores nil) local myTable = { a, b, c } for index = 1, #myTable then if myTable[index].value == nil then -- Found a nil value. end end

The above is a bit more work and might be a breaking change for you, but if it proves useful, then have at it.  :slight_smile: