Concatenation and Length Operators

From PZwiki
Jump to: navigation, search
Language policy Language: [[::Concatenation and Length Operators|English]]


This short tutorial will cover two useful operators that haven't be mentioned so far.

Concatenation Operator

Concatenation is the process of splicing together two values (strings or numbers) into a string, and is represented by two dots: ..
For example:

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.")

Length Operator

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.

Summary

  • Concatenation operator splices together two numbers or strings into a string
  • Length Operator returns the length of either a string or table