How to represent nil in a table

Suppose I want to keep a list of items. Including some nil values. The position of the values ​​is significant, and I need to imagine the absence of a value in the list at this position.

Here is the problem:

a = {1,2,3,nil,4}

for k,v in ipairs(a) do
  print(k,v)
end

print(a[4]) 
print(a[5])

The for loop will print only elements 1,2 and 3. It stops at zero. The first print statement prints zero, but I'm not sure if it is actually stored in the table or not. (Who knows?) The second print statement prints 4 - as expected.

So, here is the question: how to present a list of elements in a table and efficiently execute them? Given the above conditions, for example, the position is significant, and some positions are β€œempty”. In other words: it does not matter, but the absence of this value in this position makes sense.

+4
5

"null.lua"

local function null(...)
   local t, n = {...}, select('#', ...)
   for k = 1, n do
      local v = t[k]
      if     v == null then t[k] = nil
      elseif v == nil  then t[k] = null
      end
   end
   return (table.unpack or unpack)(t, 1, n)
end
_G.null = null

null()

require("null")

a = {null(1,2,3,nil,4)}
-- the same could be done element-by-element
-- a = {null(1),null(2),null(3),null(nil),null(4)}

for k,v in ipairs(a) do
   v = null(v)
   print(k,v)
end

print(null(a[4]))
print(null(a[5]))
+3

Lua , , "". Lua , : .

, ( ipairs()) . nil , nil. ": ".

, , . - "nil" , nil.

a = {1, 2, 3, "nil", 4}

for k,v in ipairs(a) do
  print(k,v)
end

:

1   1 
2   2
3   3
4   nil
5   4

- , Lua , "nil" nil.

"" ( ) "" "Lua", 5 "". Roberto Ierusalimschy , .

- . .

function makeList(...)
  local list = table.pack(...)

  list.length = 
    function(self) return self.n 
    end

  list.append = 
    function(self, value)
      self.n = self.n + 1
      self[self.n] = value
    end

  list.print = 
    function(self)
      for i = 1, self.n do print(i, self[i]) end
    end

  return list
end

a = makeList(1, 2, 3, nil, 4)
a:append(5)

a:print()

print(a:length())

:

1   1
2   2
3   3
4   nil
5   4
6   5
6

: table.pack 'n', , "nil" . . 6.2 PIL, Variadic Functions.

+3

- , . "" ipairs ( ), :

function create(...)
    local t = table.pack(...)
    local self = {
        num = t.n,
        elements = { ... }
    }
    return self
end

function elements(t)
    local f = function(s, i)
        i = i + 1
        if i <= s.num then
            return i, s.elements[i]
        end
    end
    return f, t, 0
end

local seq = create(1, 2, nil, 3)

print(seq.num)
for i, e in elements(seq) do
    print(i, e)
end
-- results:
-- 4
-- 1    1
-- 2    2
-- 3    nil
-- 4    3

, IP-, .

+1

, " " . ( : #, , nil), , .

+1

, nil .

- .

local mynil = {} -- every new table is unique!

a = {1,2,3,mynil,4}

for k,v in ipairs(a) do
  if (v == mynil) then
    v = nil
  end
  print(k,v)
end

More problems with the nil string, which can also be stored in a table, another comparison is a secondary problem. ipairsor any other iterator will show that a key with a value mynilexists. This means that you can share existence mynilwith a missing key =nil.

PS If you want to move your list, you can consider the function table.remove(list, key).

0
source

Source: https://habr.com/ru/post/1659943/


All Articles