lua learning record | CSDN creation punch in

Posted by postalservice14 on Thu, 03 Feb 2022 08:54:00 +0100

1. Configuring lua development environment with vscode

Download the required compiler:

  • Download from official website http://www.lua.org/home.html

    Click this to enter the download (you can see that he said this way if you use win), but I can't open it. I don't know why

    After reading the articles of other bloggers, you can choose to download them directly from GitHub: https://github.com/LuaDist/Binaries

    That's it. It looks really old, but it seems that it's all this. After downloading and decompressing, there are four folders in it

    Enter the bin directory, copy the current path, and add the path to the user variable and system variable

    The following is the path of the user variable

    The following is the path of the system variable

    Print the version number to test whether the installation is successful. Open cmd to print

    Install the tools needed to run the script in vscode

    After installation, close vscode and restart. I actually need to restart here, otherwise an error will be reported!!!

Let's start hello world


Print succeeded, ok installation completed

If you do not use vscode for development, you can also use a browser. The link is as follows:
https://wiki.luatos.com/pages/emulator.html

The open page is as follows. The upper part is the window for running and stopping, and the lower part is the output window for printing. The effect is as follows

Then, if lua studies, I recommend watching this video. My following records are basically based on the video records. I ran it myself
https://www.bilibili.com/video/BV1vf4y1L7Rb

30 minutes, one hour for actual measurement

2. Basic grammar

notes

Use – to annotate, just two horizontal bars

-- print("hello world") notes

Variables are mainly divided into private variables and global variables

a = 1 -- The default is a global variable
b = 2

local a = 3  -- Private variable of current file

No error will be reported when printing variables that do not exist. This is the case in many places. Nil is returned, and nil is considered as logical false in lua

print(c) -- No variable is nil



Multiple variables can be assigned together. This assignment is a bit like python

-- a = b = c = 1 -- This assignment won't work
a,b,c = 1,2,1
print(a,b,c) -- This is ok Yes, similar py

Variables also support hexadecimal and scientific counting

-- Followed by hexadecimal
a = 0x11
-- -- Scientific counting method
b = 2e10
print(a,b)

As for data types, lua does not support all kinds of numeric types. Integer floating-point types are collectively referred to as number types

Variable calculation is basically supported like other languages

a = 0x11
-- Scientific counting method
b = 2e10
print(a+b) -- Addition operation
print(a*b)
print(a/b)
print(a-b)
print(10^5) -- exponentiation 

3. String

Like python, string definition can be defined directly. It also supports escape characters. String connection adopts two points

a = "fdsfa\nsffds" -- Support escape characters
b = "fafsaf"
print(a)
print(a..b) -- String connection


To use a format string with different lines, you need to use two brackets

c = [[
    fvgsgsd
    gsdagas
    gsdg\nsffds
]]
print(c) -- Output characters of different lines


String to number conversion

-- String digit conversion

c = tostring(10) --Convert numeric value to string
n = tonumber("10") --reverse

print(c)
print(n)


Use # to get the length of the string

a = "dfgfgsgfg"
print(#a) -- get the length of the string

4. Function

Using function to create a function feels a bit like matlab. The difference between lua and python is that it doesn't depend on this. Therefore, it ends with end.
The standard format of the function is as follows

function function_name( ... ) -- A new basic function, function, function name and function parameters
    -- body
end

Let's try to create a function

function f( a,b,c ) -- Use function
    return a,b
    -- body
end

local i,j = f(1,2)
print(i,j)

5. table (array)

This I understand is a more awesome array

You can see that the internal elements can be numbers, characters or functions. Use [] to index to obtain the value of the element, and you can add or delete elements

a = {1,"ac",{},function()end} -- There are many things you can store
print(a[1],a[2]) --Use the index to print the element. If there is no index, it will be printed nul
-- But it can be added. There is no limit to this
a[5] = 123
print(a[5])
print(#a) --d print length


Insert and delete elements

-- Use built-in functions
a = {1,"ac",{},function()end}
table.insert(a,2,"d") -- Insert element
table.remove(a,2,"d") -- Delete the element, and this can be returned as a return value

Other methods of indexing elements

a = {
    a = 1,
    b = "1234",
    c = function()

    end,
    d = 123123,
    [",;"] = 123
}
print(a["a"]) -- Print the value corresponding to the subscript
print(a.b) -- Another way is to index
print(a.a)

print(a[",;"]) -- Print named Subscripts

a["abc"] = "afsaf"
print(a["abc"])


The global table can be understood as a memory, that is, the defined elements are his, so the global table can be used for indexing. The name is_ G[]

a = 10
print(_G["a"]) --a stay gļ¼›inside


You can see that there is no error in printing, ok!!!

6. Logical value

Many expressions of logical values are supported. Note that 0 is true rather than false. It is expressed in the last sentence

a = true
b = false
print(1>2)
print(1<2)
print(1>=2)
print(1<=2)
print(1==2)
print(1~=2)

print(a and b)
print(a or b)
print(not b)

a = nil -- false 
b = 0 --really
print(a==true)
if b then
    print("b is true")
end


Use logical values to achieve the effect of type trinomial expressions

b = 0
print(b>10 and "yes" or "no") -- Achieve the effect similar to three term expression

7. Select structure

It is the effect of if then else end. Then and end are added to the original structure, because they are distinguished from each other by end

-- Branch judgment statement
if 1>10 then
    print("1>10")
elseif 1<10 then
    print("1<10")
else
    print("no")
end

8. Cyclic structure

-- for loop

for i =1,10,2 do -- Initial value, end, step size
    print(i) -- Recirculation internal i The value cannot be modified
    if i == 5 then -- have access to break Terminate cycle
        break
    end
end

-- while loop

local n = 10
while n>1 do
    print(n) -- Same support break
    n = n-1 -- Loop variables are modified internally
end

Topics: lua