Continued with Expressions topic:
3.4 – Concatenation
Lua denotes the string concatenation operator by "..
" (two dots). If any of its operands is a number, Lua converts that number to a string.print("Hello " .. "World") --> Hello World print(0 .. 1) --> 01Remember that strings in Lua are immutable values. The concatenation operator always creates a new string, without any modification to its operands:
a = "Hello" print(a .. " World") --> Hello World print(a) --> Hello
3.5 – Precedence
Operator precedence in Lua follows the table below, from the higher to the lower priority:^ not - (unary) * / + - .. < > <= >= ~= == and orAll binary operators are left associative, except for `^
´ (exponentiation) and `..
´ (concatenation), which are right associative. Therefore, the following expressions on the left are equivalent to those on the right:a+i < b/2+1 <--> (a+i) < ((b/2)+1) 5+x^2*8 <--> 5+((x^2)*8) a < y and y <= z <--> (a < y) and (y <= z) -x^2 <--> -(x^2) x^y^z <--> x^(y^z)When in doubt, always use explicit parentheses. It is easier than looking up in the manual and probably you will have the same doubt when you read the code again.
3.6 – Table Constructors
Constructors are expressions that create and initialize tables. They are a distinctive feature of Lua and one of its most useful and versatile mechanisms.The simplest constructor is the empty constructor,{}
, which creates an empty table; we saw it before. Constructors also initialize arrays (called also sequences or lists). For instance, the statementdays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}will initializedays[1]
with the string"Sunday"
(the first element has always index 1, not 0),days[2]
with"Monday"
, and so on:print(days[4]) --> WednesdayConstructors do not need to use only constant expressions. We can use any kind of expression for the value of each element. For instance, we can build a short sine table astab = {sin(1), sin(2), sin(3), sin(4), sin(5), sin(6), sin(7), sin(8)}To initialize a table to be used as a record, Lua offers the following syntax:a = {x=0, y=0}which is equivalent toa = {}; a.x=0; a.y=0No matter what constructor we use to create a table, we can always add and remove other fields of any type to it:w = {x=0, y=0, label="console"} x = {sin(0), sin(1), sin(2)} w[1] = "another field" x.f = w print(w["x"]) --> 0 print(w[1]) --> another field print(x.f[1]) --> another field w.x = nil -- remove field "x"That is, all tables are created equal; constructors only affect their initialization.Every time Lua evaluates a constructor, it creates and initializes a new table. Consequently, we can use tables to implement linked lists:list = nil for line in io.lines() do list = {next=list, value=line} endThis code reads lines from the standard input and stores them in a linked list, in reverse order. Each node in the list is a table with two fields:value
, with the line contents, andnext
, with a reference to the next node. The following code prints the list contents:l = list while l do print(l.value) l = l.next end(Because we implemented our list as a stack, the lines will be printed in reverse order.) Although instructive, we hardly use the above implementation in real Lua programs; lists are better implemented as arrays,
No comments:
Post a Comment