• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua weird behavior on lua table size.

drakylucas

Intermediate OT User
Joined
Dec 15, 2015
Messages
236
Solutions
7
Reaction score
121
Hi all,

I don't know if it should be in support, or discussion

why does lua have this weird behavior?

Lua:
local table0 = {}

local table1 = {
[0] = "xx",
}

local table2 = {
[1] = "xx",
}

local table3 = {
[0] = "xx",
[1] = "yy",
}

local table4 = {
[0] = "xx",
[1] = "yy",
[2] = "yy",
}

local table5 = {
[1] = "xx",
[2] = "yy",
[3] = "yzy",
}

print("table0: " ..#table0)
print("table1: " ..#table1)
print("table2: " ..#table2)
print("table3: " ..#table3)
print("table4: " ..#table4)
print("table5: " ..#table5)

this will print this out:

table0: 0
table1: 0
table2: 1
table3: 1
table4: 2
table5: 3
if the table has an item with index "[0]", it is not counted in tablesize (using "#" symbol)
is there any reason for this to happen?
I'm just curious.
 
Solution
Hi all,

I don't know if it should be in support, or discussion

why does lua have this weird behavior?

Lua:
local table0 = {}

local table1 = {
[0] = "xx",
}

local table2 = {
[1] = "xx",
}

local table3 = {
[0] = "xx",
[1] = "yy",
}

local table4 = {
[0] = "xx",
[1] = "yy",
[2] = "yy",
}

local table5 = {
[1] = "xx",
[2] = "yy",
[3] = "yzy",
}

print("table0: " ..#table0)
print("table1: " ..#table1)
print("table2: " ..#table2)
print("table3: " ..#table3)
print("table4: " ..#table4)
print("table5: " ..#table5)

this will print this out:


if the table has an item with index "[0]", it is not counted in tablesize (using "#" symbol)
is there any reason for this to happen?
I'm just curious.

The # keyword is to count...
Hi all,

I don't know if it should be in support, or discussion

why does lua have this weird behavior?

Lua:
local table0 = {}

local table1 = {
[0] = "xx",
}

local table2 = {
[1] = "xx",
}

local table3 = {
[0] = "xx",
[1] = "yy",
}

local table4 = {
[0] = "xx",
[1] = "yy",
[2] = "yy",
}

local table5 = {
[1] = "xx",
[2] = "yy",
[3] = "yzy",
}

print("table0: " ..#table0)
print("table1: " ..#table1)
print("table2: " ..#table2)
print("table3: " ..#table3)
print("table4: " ..#table4)
print("table5: " ..#table5)

this will print this out:


if the table has an item with index "[0]", it is not counted in tablesize (using "#" symbol)
is there any reason for this to happen?
I'm just curious.
Lua arrays start at 1 instead of 0.

You can technically start them at any number you want, but some standard functions become unreliable if the index doesn't start at 1.

 
The interesting thing is:
Code:
print(table1[0])
shows "xx"

so lua knows that there is something on index 0, but it doesn't count on counting table elements (#table1 returns 0)
 
Hi all,

I don't know if it should be in support, or discussion

why does lua have this weird behavior?

Lua:
local table0 = {}

local table1 = {
[0] = "xx",
}

local table2 = {
[1] = "xx",
}

local table3 = {
[0] = "xx",
[1] = "yy",
}

local table4 = {
[0] = "xx",
[1] = "yy",
[2] = "yy",
}

local table5 = {
[1] = "xx",
[2] = "yy",
[3] = "yzy",
}

print("table0: " ..#table0)
print("table1: " ..#table1)
print("table2: " ..#table2)
print("table3: " ..#table3)
print("table4: " ..#table4)
print("table5: " ..#table5)

this will print this out:


if the table has an item with index "[0]", it is not counted in tablesize (using "#" symbol)
is there any reason for this to happen?
I'm just curious.

The # keyword is to count values between 1 to "n".

---

If you want to count all elements within a table, independently of the keys, you should keep it registered in another location (if need too much efficiency).

If you don't need it (which is probably), you can simply use a common table function:

Lua:
function table.size(t)
  local size = 0
  for _, _ in pairs(t) do
    size = size + 1
  end
  return size
end

See this example:
Lua:
local t =
{
  0, -- [1] = 0
  1, -- [2] = 1
  nil, -- Does not count! -- [3] = nil
  'hello!!', -- [4] = 'hello!!'

  [_t] = 'msg', -- [table_reference] = 'msg'

  hi = 'hi', -- ['hi'] = 'hi'
  ['hiho!'] = 'hiho!', -- ['hiho!'] = 'hiho!'

  [7] = 7, -- [7] = 7
  [-1] = -1, -- [-1] = -1
}

print(table.size(t)) -- Prints 8 (since nil does not count)
 
Solution
Back
Top