Concatenation and Length Operators
This short tutorial will cover two useful operators that haven't be mentioned so far.
Concatenation is the process of splicing together two values (strings or numbers) into a string, and is represented by two dots: ..
a = "Very " .. "handy" print(a)
You can concatenate variables too, if they contain strings or numbers:
a = "This also " b = "works" c = a .. b print(c)
d = 25 .. 25 print(d) print(type(d))
Note that the output is always a string. Although remember Lua can convert strings containing only numbers into a number if is attempted to be used as a number:
d = "25" .. 25 print(d, type(d)) d = d * d print(d, type(d))
Within PZ, this is useful for menus or any seen text that uses a value that may vary by player. For example:
playerName = "Baldspot" daysSurvived = 7 zedsKilled = 15 print(playerName .. " survived for " .. daysSurvived .. " days. He killed " .. zedsKilled .. " zombies.")
The length operator is self-explanatory; it returns the length of it's operand. It is unuary and represented with: #
a = "Baldspot was here" print("The sentence is " .. #a .. " characters long")
It can only be used for strings and tables.
- Concatenation operator splices together two numbers or strings into a string
- Length Operator returns the length of either a string or table