First five chapters of lua study

Basic concepts

-l parameter

-- a,b All documents need to 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 first a.lua ,Rerun 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 the current line of the file:" .. list.value)
	print("Current line value of the file:" .. list.value)
	-- Because for the first time list by nil,So it needs to be converted
	--print("The value The value is saved to a value named list.next Of table , which is referenced as:" .. 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("Because after reading the file data circularly, value The value of the last line of the file is saved, so 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)) -- And the last list Reserved table Consistent references
print(list_next == l.next) -- true
--print("At this time, the current table Held by next Reference changed to list.next ,That is to say, there is a previous one list Of table")
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. v gets the value of the original table as the index of the new table
    3. k gets the index of the original table to the value and assigns it to the new table as its value

function

  1. unpack is used to deconstruct a table and get all the elements in a table. It is very useful to overload the 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"}

Tags: Programming

Posted on Mon, 20 Apr 2020 09:46:53 -0700 by phil88