lua学习前5章解惑

lua学习前5章解惑

基础概念

-l 参数

-- a,b文件均需放到 lua.exe 所在的文件目录
-- a.lua 文件
x = 5
-- b.lua 文件
print(x)
-- 交互模式中输入
lua -la -lb 
-- 先运行 a.lua ,再运行 b.lua
-- 结果为 5

表达式

链表

list = nil
local rows = 1
list_next = nil
for line in io.lines("D://LuaStudy//CodeTest//day3//链表数据.txt") do
    list = {next = list, value = line}
    --print("文件的当前行数:" .. rows)
    print("The number of rows in the file:" .. rows)
    --print("文件的当前行的值:" .. list.value)
    print("Current line value of the file:" .. list.value)
    -- 由于第一次 list 为 nil,所以需要进行转换
    --print("将文件所在行的 value 值保存到一个名为 list.next 的 table 中,其引用为:" .. 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("因为循环读文件数据后,value 保存的是文件最后一行的值,所以 l.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)) -- 和最后一个 list 所保留的 table 引用一致
print(list_next == l.next) -- true
--print("此时当前 table 持有的 next 引用改变为 list.next ,也就是保存有上一个 list 的 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

语句

  1. 不论是赋值还是循环赋值,其对表达式或值的运算都是在其赋值之前一次性求值的
    1. 赋值时,会先计算右边的值
    2. 循环时,三个表达式在循环前求值
  2. 通过迭代器生成一个逆向原有索引的 table 很容易
    1. 就是将原有 table 的值和索引互换
    2. v 获取原 table 的值,作为新 table 的索引
    3. k 获取原 table 对于值的索引,赋值给新 table 作为其值

函数

  1. unpack 用来解构一个 table ,得到一个 table 中的所有元素,在重载参数列表时很有用

    1. 只需将解构出来的元素作为某个函数的参数即可
    2. 如:string.find(unpack({"bilibili", "bi"}))
  2. 具名参数可指定某个函数参数为 table,并在调用时传入这个 table 给期内的参数命名

function rename (arg)
    print(arg.old, arg.new)
    return os.rename(arg.old, arg.new)
end
rename{old = "D:\\LuaStudy\\CodeTest\\4——函数\\temp.lua", new = "D:\\LuaStudy\\CodeTest\\4——函数\\temp1.lua"}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 8,043评论 2 9
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 10,025评论 0 5
  • 第六日笔记 1. 基础概念 程序块 定义 在 lua 中任何一个源代码文件或在交互模式中输入的一行代码 程序块可以...
    跃千山阅读 2,555评论 0 0
  • 函数有两种用途: 完成指定任务,此时函数作为调用语句使用。 计算并返回值,此时函数作为赋值语句的表达式使用。 调用...
    JunChow520阅读 9,158评论 0 3
  • 从0开始学lua,给自己记一个笔记,使用书籍 lua程序设计(第二版),电子工业大学出版社 lua的中文API) ...
    Charon_ted阅读 2,849评论 0 0

友情链接更多精彩内容