Please enable / Bitte aktiviere JavaScript!
Veuillez activer / Por favor activa el Javascript![ ? ]
Can if in lua to check for the presence of elements in the array? - bug-code.com Q&A

Can if in lua to check for the presence of elements in the array?

0 like 0 dislike
10 views
Good afternoon, gentlemen.
Can if in lua to check there in the declared array values? In other words, is equivalent to the code:
local currentTable = {} if currentTable then

Here is the code:
local currentTable = {} if #currentTable ~= 0 then

And what is the comparison faster?

UPD. In addition, the second option will not work correctly if the elements in the table are not presented sequentially. And in this case will work the first option?
asked by | 10 views

1 Answer

0 like 0 dislike
Are not equivalent. The code below will check whether there is currentTable. if devoid of meaning, because currentTable declared and exists:
local currentTable = {} if currentTable then

The code below will currentTable elements in order, and count their number.
local currentTable = {} if #currentTable ~= 0 then

In the case of:
local a = "name" local currentTable = {a="eman"} if #currentTable ~= 0 then

if again doesn't make sense, because the first element of the table currentTable will be a name element meaning "eman". Nothing to consider, the counter elements #currentTable will show 0 because currentTable[1] = nil.

The check implemented like this:
function IsEmpty(t) if not t then return true end for _, _ in pairs(t) do return false end return true end local currentTable = {} if not IsEmpty(currentTable) then
answered by
Welcome to Bug-code Q&A, where you can ask questions and receive answers from other members of the community.

24.8k questions

46.2k answers

0 comments

12.7k users

24,788 questions
46,220 answers
0 comments
12,715 users