Lua study notes 9: Multi-file, lua study notes 9
Execute multiple files in a terminal:-l
Add a variable defined in file 1 and output the variable in another file. The Code is as follows:
--file1.luanum = 100
--file2.luaprint(num)
Terminal input (Note: it is not the lua command line ):
lua -lfile1 -lfile2
Note: Do not add the file suffix. lua
Load files in command line
--lib.luafunction norm(x, y) local n2 = x^2 + y^2 return math.sqrt(n2)endfunction twice(x) return 2 * xend
In the lua command line, enter:
dofile("lib.lua")n = norm(2.1, 1.4)print(twice(n))
How can LUA introduce parameters in multiple files at the same time?
Assume that the. lua and B. lua files are in the same directory.
B. Content of the lua file:
Function func ()
Print ("Hello world! ")
End
A. lua file content:
Require "B"
Func ()
In this way, you can call the function of the B. lua file.
Lua provides a complete code for reading and writing files. In addition, there are examples of code that are more practical than LUA. The more you give, the more points you give.
1.txt File
------------------------
12345
67890
------------------------
Read. lua read
-----------------------
Local file = io. open ("1.txt ")
For l in file: lines () do
Print (l)
End
File: close ()
----------------------
Write. lua write
----------------------
Local file = io. open ("2.txt"," w ")
File: writes ("12345 ")
File: close ()
---------------------------------------
Ps: Netizens answer questions with the purpose of helping you answer questions and help each other, rather than showing your points.
In addition, there won't be any questions that even have no initial score. How many points do you expect?