Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if a variable can be printed in Lua

Tags:

string

types

lua

I've got a variable that could be a number of types - sometimes it's a string, sometimes a number, table or bool. I'm trying to print out the value of the variable each time like this:

print("v: "..v)

with v being my variable. Problem is, when I get a value that can't be concatenated I get this error:

myscript.lua:79: attempt to concatenate a table value

I've tried changing it to this in case it manages to detect whether or not the variable can be printed:

print("v: "..(v or "<can't be printed>"))

but I had the same problem there. Is there some sort of function I can use to determine if a variable can be concatenated to a string, or a better way of printing out variables?

like image 944
benwad Avatar asked Dec 08 '22 15:12

benwad


1 Answers

You can provide the values as separate arguments to print:

print("v:", v)

This would print something like

v:  table: 006CE900

Not necessarily the most useful, but better than a crash if it's just for debugging purposes.

See here for information on more useful table printing.

like image 164
Supr Avatar answered Dec 27 '22 17:12

Supr