Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty variable check [duplicate]

Tags:

variables

lua

I'm just learning lua, this is my first script with it. How can I check to see if a variable is empty or has something like a line feed in it?

like image 258
Anthony Kernan Avatar asked Feb 04 '12 13:02

Anthony Kernan


1 Answers

You can check whether the value is nil:

if emptyVar == nil then
   -- Some code
end

Since nil is interpreted as false, you can also write the following:

if not emptyVar then
   -- Some code
end

(that is, unless you want to check boolean values ;) )

As for the linebreak: You can use the string.match function for this:

local var1, var2 = "some string", "some\nstring with linebreaks"
if string.match(var1, "\n") then print("var1 has linebreaks!") end
if string.match(var2, "\n") then print("var2 has linebreaks!") end
like image 121
Henrik Ilgen Avatar answered Nov 20 '22 19:11

Henrik Ilgen