Skip to content

Syntax and Data Types

Comments

Comments are lines in a Lua source code file that are ignored when executing the script. In Lua, comments are introduced by two leading minus-signs (--) and normally end with the end-of-line.

-- Comment 1
-- Comment 2

Numbers

Numbers are always floating point numbers of double precision, there is no integral number type in Lua. This means, that they are always decimal numbers and 4 and 4.0 are identical.

3.4
10e10

Logical values

Logical values (Boolean Values, Truth values) are named true and false in Lua. These values are typically the results of comparisons (3<4), logical operations (3<=4 and a<5 and not b).

Character Sequences / Strings

Character Sequences / Strings are delimited with a single quote ' or a double quote " .

"I say Hello World!"
'I say "Hello World!"'

Lua-Tables

Lua knows objects in the form of the Lua data type “table“ (should not be confused with CadnaA-tables). Fundamentally, Lua tables are associating “keys” with values. This is easily understood by example. The general syntax is

mytable = {key1 = val1, key2 = val2, ....}
-- assigning to table
mytable.key1 = val1
mytable["key1"] = val1 -- alternatively
-- accessing elements of table
val1 = mytable.key1
val1 = mytable["key1"] -- alternatively

The key-types can be numbers or strings, and values can be any kind of Lua value/variable. The value of key products in this Lua table is a special case: an array. Arrays are normal Lua tables with integer numbers for keys. To access the elements of an array use the bracket notation.

cont_info = {company= "DataKustik GmbH", 
    town= "Gilching", 
    products= {"CadnaA", "CadnaR"}}
cna.print(cont_info.company)
cna.print(cont_info.town)
cna.print(cont_info.products[2])

cont_info.company2 = "BMW AG" -- alternative 1
cont_info.town2 = "Munich"
cont_info.products2 = {"323", "525", "740", "X1", "X5"}
cna.print(cont_info.company2)
cna.print(cont_info.town2)
cna.print(cont_info.products2[2] ..'  '.. 
    cont_info.products2[4])
cna.print(cont_info['company']) -- alternative 2
cna.print(cont_info['town'])
cna.print(cont_info['products']) -- doesn‘t work with arrays!

The number of elements in an array can be determined by the # operator. I.e. #cont_info.products is “number of products”.

nil

There is one value that denotes the absence of data and it is represented by the keyword nil.