Lua學習筆記Day1 Lua標準庫函式

2021-07-11 20:45:09 字數 2389 閱讀 3776

本文內容來自lua官方文件

math.pi 返回pi的值、

math.huge 返回乙個最大數

math.abs(x) 返回x的絕對值

math.ceil(x) 向上取整

math.floor(x) 向下取整

math.fmod(x,y) x/y取模,注意小數

math.modf (x) 取x的整數和小數部分

math.sqrt(x) 返回x的開平方

math.pow(x,y) 返回x的y次冪

math.exp(x) 返回e的x次冪

math.frexp(m) m = x*(2^y),把m拆成x和y兩部分

math.ldexp(x,y) 返回x*(2^y)

math.random(x,y) 沒有引數返回[0,1]之間的數,乙個整數引數m返回[1,m]之間的整數

math.randomseed(x) 設定隨機數種子

os.time() 返回當前系統時間距離2023年1月1日0秒的秒數,可以傳乙個引數

os.date() 返回本地化的時間字串

math.deg(x) 弧度轉角度

math.rad(x) 角度轉弧度

math.cos(x) 返回x的余弦值,結果是弧度

math.cosh(x) 返回雙曲線余弦值

math.sin(x) 返回x的正弦值,結果是弧度

math.sinh(x) 返回雙曲線正弦值

math.acos(x) 返回x的反余弦值,結果是弧度

math.asin(x) 返回x的反正弦值,結果是弧度

math.atan(x) 返回x的反正切值,結果是弧度

math.atan(y,x) 返回y/x的反正切值,結果是弧度,x是0也會正確處理

string.len(str) 返回str的長度 

string.lower(str) string.upper(str) 把str轉化為小寫|大寫

string.byte() str:byte(1,3) 返回字串str中從第1位到第3位的ascii值

string.char() string:char(49,50,51) 返回ascii值對應的字串

string.dump(function) 返回指定函式的二進位制**(函式必須是乙個lua函式,並且沒有上值)

string.find(s, pattern [,init [,plain]])

string.find(s,"abcd",1,plain) 查詢s中首次出現"ab"的位置,從1位置開始,如果找到則返回首次出現的起始和結束位置(1,4)否則返回nil

string.format(formatstring,...) 格式化字串

string.format('%s', 'a string with "quotes" and \n new \t line')

結果:a string with "quotes" and

new line

%q:為自動將對應引數字串中的特殊字元加上\

string.match(s,pattern,init) 查詢s中從init開始是否有pattern,如果有則返回pattern

string.gmatch(s,pattern) 返回乙個迭代函式,每次呼叫此函式,將返回下乙個查詢到的樣式串對應的字元

s = "hello world from lua"

for w in string.gmatch(s, "%a+") do

print(w)

end結果:

hello

world

from

luastring.sub(s, i [,j]) 字串擷取i位置和j位置之間的字串

string.gsub(s, pat, repl [,n]) 將字串中的pat替換為repl,返回字串及替換的次數

string.rep(s, n) 把字串s重複n次

string.reverse(s) 返回s的反轉

table.concat(table,sep,start,end) 如果乙個表裡都是字串或數字,用concat可以把它們連線起來,sep是分割符

table.insert(table, [pos,]value) 把value插入到table的pos位置,pos為空表示尾部插入

table.remove(table[,pos]) 移除table表pos位置的元素

table.sort(table [,comp]) table排序,預設順序是數字從小到大,字母abc...

table.sort(network,function(a,b) return(a.name < b.name) end)

table.maxn(table) 返回table的最大索引

Lua學習筆記 1

前段時間一直在尋找,究竟c 中嵌入哪種指令碼語言比較好呢?python,ruby,lua,perl 後來,開源社群的朋友很多都推薦lua,於是我也就開始學習lua程式設計.我的開發環境是vs.net 2003,新建乙個空的c console專案,再新建乙個main.cpp檔案,然後新增以下 incl...

Lua學習筆記1

今天上班的時候和程式對乙個功能的程式介面,需求是這樣的 指令碼 根據配置檔案生成一組中獎資訊,用table來儲存,然後將這個table傳給程式介面,程式根據這個table中的資料向玩家顯示中獎介面。很簡單的乙個功能,指令碼這邊的函式很快就寫完了,然後我就琢磨著程式的介面應該怎麼寫,由於之前沒有試過在...

Lua學習筆記 1

lua也是一種指令碼語言,和shell類似。學習這個是因為在使用redis做分布式鎖的時候有一種lua的寫法,所以就簡單的學習一下這個指令碼語言。其實但凡是指令碼語言都有一種執行方式就是互動式 什麼叫做互動式?就是類似於問答模式,輸入一條命令輸出結果 直接輸入lua,就進入了互動介面。可以自己玩一下...