One of lua's introductory notes

Posted by dominicd on Tue, 08 Feb 2022 21:44:37 +0100

preface

lua is a powerful, efficient, lightweight and embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming and data description. I study lua mainly because of its perfect coordination mechanism.

1. Installation lua environment

The installation process is relatively simple. My Ubuntu 18 lua5.04 is installed by default Version 15. I downloaded the latest lua version 5.4.4 from the official website again.

wget http://www.lua.org/ftp/lua-5.4.4.tar.gz
cd lua-5.4.4/
make
make test
make install

Run lua directly after the installation is completed. If the installation is successful, you can enter the lua command line mode

root@:/home/learn/lua_test# lua
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> 

Simply record the exit method

ctrl + c
ctrl + d
 Command line input: os.exit()

2 lua script

lua script is named in the same way lua end file.
vi firstlua.lua code is as follows

function printhello ()
    print("hello world")
end

Open the command line to execute and see the effect: use the dofile function to load the script into the system, that is, you can call the internal functions of the script.

root:/home/learn/lua_test# lua
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> dofile("firstlua.lua")
> printhello()
hello world

3 basic grammar

3.1 lua keyword

The following are the keywords reserved by lua. Note that the keywords are case sensitive.

and       break     do        else      elseif    end
false     for       function  goto      if        in
local     nil       not       or        repeat    return
then      true      until     while

3.2 notes

Annotation syntax

-- Line comment
--[[
multiline comment 
--]]

3.3 type and value

There are eight official types: nil (empty), boolean (boolean), number (numeric), string (string), function (function), userdata (user data), thread (thread) and table (table).
Nil is a type with only one nil value. Its main function is to distinguish it from all other values.
boolean in condition detection, Lua language regards zero and empty strings as true, and false or nil represents all false values.
And and or follow the principle of Short-circuit e valuation, that is, the second operand is evaluated only when necessary. And has priority over or.

3.4 math function

Trigonometric function: sin,cos,tan,asin
 Random number: random
 Constant: pi,huge
 Rounding: floor Rounding down ceil Round up modf Round to zero
 Range: maxinteger,mininteger

3.5 symbol priority

Priority from low to high

or
and
 <     >     <=    >=    ~=    ==
 |
 ~
 &
 <<    >>
 ..
 +     -
 *     /     //    %
 Unary operator (not   #     -     ~)
 ^

3.6 string

Length calculation

--utilize#prefix
> print(#"hello")
5

Multiline string

> page = [[ <hello>
>> <world>]]
> print(page)
 <hello>
<world>

String function

--copy
> string.rep("hello ", 3)
hello hello hello 
-- Flip
> string.reverse("hello")
olleh
--Turn lowercase
> string.lower("Hello")
hello
--Capitalize
> string.upper("Hello")
HELLO
--Find subsets, take the first to last subsets
> string.sub("Hello", 1, -1)
Hello
--ASCII transformation
string.char and string.byte
--format
string.format("x = %d y = %d", 10, 20)
--Find string
> string.find("Hello", "ll")
3       4
--Find string and replace
> string.gsub("Hello", "ll", "--")
He--o   1
--utf8 Character operation
> s = utf8.char(114, 222, 115, 116, 109)
> print(s)
rÞstm
> utf8.codepoint(s, 1, -1)
114     222     115     116     109

Table 3.7

Table is the most important and powerful data structure in Lua language. Using tables, Lua language can represent arrays, sets, records and many other data structures in a simple, unified and efficient way.

--Table creation
> a = {}
--Insert key value pair
> a["x"] = 111
> a["x"]
111
> a[1] = "xxx"
> a[1]
xxx
--Delete key value pair
> a[1] = nil
> a[1]     
nil

Table constructor

--Automatically add index
> week = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"}
> week[3]
wed
--Manually set key value pairs
> test = {i = 1, j = 2}
> test.j
2

Traversal table

> for k, v in pairs(week) do
>>     print(k, v)
>> end
1       mon
2       tue
3       wed
4       thu
5       fri
6       sat
7       sun

Table standard function

--insert
> table.insert(test, 1, 3)
> table.insert(test, 4)
> for k, v in pairs(test) do
>>     print(k, v)
>> end
1       3
2       4
j       2
i       1
--remove
> table.remove(test, 1)
3
--move
> print(#t)
3
> table.move(t, 1, #t. 2) -- move the first element to the last element to the position of the second element, which is equivalent to adding a new element
table: 0x555db8679ba0
> print(#t)
4

3.8 functions

Variable parameter function

function add(...)
    local s = 0
    for _, v in ipairs{...} do
        s = s + v
    end
    return s
end

print(add(1, 2, 3))

recursion
The special reason is that lua will not overflow the stack space

function foo(n)
    if n > 0 then
        print(n)
        return foo(n -1)
    end
end
foo(5)

Topics: lua