string.len() always returns the error "unexpected symbol"

Viewed 136
function Main(Inhalt)

print(string.len(Inhalt))
end

Main(Bla)

This is just a example, I run in multiple problems like: "input:3: bad argument #1 to 'len' (string expected, got nil)" (Like here), or anything else with unexpected.

I'm kinda new to this, so please explain it to me from ground up I'm trying to figure out for a pretty long time. I already tried to convert this to a string with tostring(), but yes I'm missing something. Thanks for your help.

3 Answers

In this case Bla either needs to be a string which you can fix by putting quotes around it

function Main(Inhalt)
   print(string.len(Inhalt))
end

Main("Bla")

or needs to be a variable that contains a string

Bla="test string"

function Main(Inhalt)
   print(string.len(Inhalt))
end

Main(Bla)

Not a lua expert but it seems like you're trying to get the length of the string value Bla. The way you've written it right now does not indicate Bla is of string type. If you change it to the following, this should work.

function Main(Inhalt)
 print(string.len(Inhalt))
end

Main("Bla")

Try this:

string1 = "Bla"
Main(string1)

In your code snippet Bla is not defined. Strings are surrounded by "".

Related