Lua study notes 9: Multi-file, lua study notes 9

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.