Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a value is empty in Lua?

What is the proper way to make a conditional which checks of something is or is not empty in Lua? if x == "" and f x ~= "" does not seem to work.

like image 354
Village Avatar asked Apr 26 '12 06:04

Village


People also ask

How do you check if a value is empty?

The IS NULL operator is used to test for empty values (NULL values).

Is null in Lua?

When programming in Lua, it is important to understand the different meanings of nil and NULL. Nil is the "unknown type" in the context of Lua whereas null and NULL represent the SQL NULL.

Is nil true in Lua?

In Lua both nil and the boolean value false represent false in a logical expression. Anything that is not false (either nil or false ) is true .


1 Answers

Lua is a dynamically type-based language.
Any variable can hold one of the following types: nil, boolean, number, string, table, function, thread, or userdata.
Any variable in a table (including _G, the table where globals reside) without a value gives a value of nil when indexed. When you set a table variable to nil, it essentially "undeclares" it (removing the entry from memory entirely).
When a local variable is declared, if it is not assigned immediately it is given a value of nil. Unlike table variable, when you set a local variable to nil, it does not "undeclare" it (it just has a value of nil).

In Lua, an empty string ("") is still a "value" - it's simply a string of size zero.

like image 90
Deco Avatar answered Sep 24 '22 18:09

Deco