First five chapters of lua study

Time:2020-6-2

First five chapters of lua study

Basic concepts

-L parameter

--A and B documents should be placed in lua.exe  File directory
--A.lua file
x = 5
--B.lua file
print(x)
--Input in interactive mode
lua -la -lb 
--Run a.lua before b.lua
--The result is 5

expression

Linked list

list = nil
local rows = 1
list_next = nil
for line in  io.lines ("D: // luastudy // codetest // day3 // linked list data. TXT") do
    list = {next = list, value = line}
    --Print ("current lines of file:... Rows)"
    print("The number of rows in the file:" .. rows)
    --Print ("value of current line of file:." list.value )
    print("Current line value of the file:" .. list.value)
    --Since the first list is nil, you need to convert it
    --Print ("save the value of the line where the file is located to a value named list.next  In table of, the reference is: "... ToString( list.next ) .. "\n")
    print("Save the value of the file's row to a table named list.next, which is referred to as:" .. tostring(list.next) .. "\n")
    rows = rows + 1
    list_next = list.next
end

local l = list

--Print ("since value stores the value of the last line of the file after reading the file data circularly, l.value is the following value ↓")
print("Because after looping through the file data, value stores the value of the last line of the file, so l.value is the following value ")
print(l.value .. "\n")
print(tostring(list_ Next) - consistent with the table reference reserved by the last list
print(list_next == l.next) -- true
--Print ("the next reference held by the current table changes to list.next  , that is, the table "with the previous list is saved.)
print("current table retains next quotes change to list.next ,Save the last one list of table")
print(l.next.value .. "\n")
print(l.next.next.value .. "\n")
print(l.next.next.next.value .. "\n")
while l do
    print(l.value)
    l = l.next
end

sentence

  1. Whether it is assignment or circular assignment, its operation on expression or value is evaluated at one time before its assignment

    1. When assigning a value, the value on the right will be calculated first
    2. When looping, three expressions are evaluated before the loop
  2. It’s easy to generate a table that reverses the original index through an iterator

    1. It is to exchange the value of the original table with the index
    2. vGet the value of the original table as the index of the new table
    3. kGet the index of the original table to the value and assign it to the new table as its value

function

  1. unpackIt is used to deconstruct a table and get all the elements in a table. It is very useful for overloading parameter list

    1. Just take the deconstructed element as the parameter of a function
    2. For example:string.find(unpack({"bilibili", "bi"}))
  2. Named parameter can specify a function parameter as table, and pass this table to name the parameter in the period when calling
function rename (arg)
    print(arg.old, arg.new)
    return os.rename(arg.old, arg.new)
end
Rename {old = "D: \ \ luastudy \ \ codetest \ \ 4 -- function\ temp.lua ", new =" D: \ \ luastudy \ \ codetest \ \ 4 -- function \ \ temp1. Lua "}"