Lua进阶指南一

Lua官方接口:LuatOS 文档

其他资料—》资料存档—》lua5.3参考手册

接口如何使用?

比如math.abs (x)

返回 x 的绝对值。(integer/float)

n = math.abs( -1 )

print(n)  --  1

--string.find (s, pattern [, init [, plain]])  s和pattern两个参数是必填的,后面两个参数是可选的,如果找到一个匹配,

--find 会返回 s 中关于它起始及终点位置的索引; 否则,返回 nil。

--第三个可选数字参数 init 指明从哪里开始搜索; 默认值为 1 ,同时可以是负值。 

--第四个可选参数 plain 为 true 时, 关闭模式匹配机制。 此时函数仅做直接的 “查找子串”的操作, 而 pattern 中没有字符被看作魔法字符。 

--注意,如果给定了 plain ,就必须写上 init 。

s1 = string.find("abcd","bc")

print(s1)  -- bc在字符串"abcd"索引的位置 2   3

s2 = string.find("abcd","bd")

print(s2)  --  nil

--多文件调用  需要用到一个函数require

--在vscode里面新建test.lua文件,在同级目录下放lua.exe;这个lua.exe可以直接调用test.lua文件

--require作用就是运行指定文件

--在同级目录下新建hello.lua文件(print("hello world"))在test.lua文件下 require("hello") hello是文件名,便可以输出hello.lua文件内容

--require用法

--[[

    1.运行指定文件

    2.末尾不带拓展名:require("hello")

    3.目录层级用"."分割:

    比如:path文件夹和test.lua文件和hello.lua文件是同级

    path

        p.lua

    test.lua

    hello.lua

    在test.lua文件里要运行p.lua文件  require("path.p")就行

    4.只会运行一次

    在hello.lua文件 

    print("hello world")

    return "done"

    在test.lua文件里

    local c = require("hello")

    print(c)  -- hello world      ;done

    在hello.lua文件

    _G.count = _G.count + 1

    print("hello world")

    return "done"

    在test.lua文件里

    _G.count = 1

    require("hello")

    require("hello")

    require("hello")

    require("hello")

    local c = require("hello")

    print(c)

    print(_G.cont)  --  2只加一次

    5.从package.path路径中查找

    把路径添加到package.path中  package.path = package.path..";./path/?.lua"  在require("p") 就行

]]

print(package.path)  --  ./lua/?.lua  这就是为什么require不需要添加文件后缀

--多次调用

--一般来说require函数只是来调用我们外部库,所以不需要多次调用

--多次调用可以使用do while等函数实现,看手册

--平常情况下如何制作我们常用的库

--比如说table.xxxxx函数如何实现

--[[

    在hello.lua文件

    local table = {}

    --如何添加函数

    function table.say()  让say函数直接位于table下

        print("hello world")

    end

    return table

    在test.lua文件里

    local table = require("hello")

    table.say()

]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值