The Mathematical Library

The math library comprises a standard set of mathematical functions, such as trigonometric functions (sincostanasinacos, etc.), exponentiation and logarithms (exploglog10), rounding functions (floorceil), maxmin, plus a variable pi
The mathematical library also defines the operator `^´ to work as the exponentiation operator.
 
math.randomseed(os.time())
math.random(10000)-----------------------1~10000
 

math.random ([m [, n]])

This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. (No guarantees can be given for its statistical properties.)

When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n]

 

The Table Library

本來想講table.getn 和 table.setn
不過在Lua5.1版
table.setn已經被棄用了(deprecated)
table.getn又被簡化成新的用法
看來那個教學網有點過期了
 
table.getn 被改成 用#井號代替
例如
a = {1,2,3,4,5}
print(#a)----> 5
 
table.insert (table, [pos,] value)
如果table的總元素是N
則pos預設是N+1
也就是直接將VALUE插入在最後面
 
table.remove (table [, pos])
似於上述
pos也預設N+1
也就是直接將最後面的元素移除
 
 
table.sort (table [, comp])
 
comp預設是由小到大排列或字典排列
for name, line in pairsByKeys(lines) do
      print(name, line)
end
function pairsByKeys (t, f)
      local a = {}
      for n in pairs(t) do table.insert(a, n) end-------------t陣列的鍵複製到a陣列
      
      table.sort(a, f)
      
local i = 0      -- iterator variable
      local iter = function ()   -- iterator function
         i = i + 1
         if a[i] == nil then return nil
         else return a[i], t[a[i]]----------------------------------a陣列的值a[i],t陣列的值t[a[i]]
         end
      end
      return iter
 end

String Library

string.find(來源字串,目標字串) 回傳找到的開頭位置,結束位置,找不到回傳Nil
string.sub(來源字串,開頭位置,結束位置) 回傳指定區間的字串
 
s = "hello world"
i, j = string.find(s, "hello")
print(i, j)                      --> 1    5
print(string.sub(s, i, j))       --> hello
print(string.find(s, "world"))   --> 7    11
i, j = string.find(s, "l")
print(i, j)                      --> 3    3
print(string.find(s, "lll"))     --> nil
string.gsub(來源字串,欲取代的來源字串,欲取代的目標字串) 
回傳1取代完的目標字串
回傳2取代次數
 
string.gfind改成gmatch(s, pattern)
s = "hello world from Lua"
     for w in string.gmatch(s, "%a+") do
       print(w)
     end
輸出:

hello
world
from
Lua

 

s = string.gsub("Lua is cute", "cute", "great")
print(s)         --> Lua is great
s = string.gsub("all lii", "l", "x")
print(s)         --> axx xii
s = string.gsub("Lua is great", "perl", "tcl")
print(s)         --> Lua is great
 
'[0-9]' is simpler when written as '%d'
'[0-9a-fA-F]' is the same as '%x'---------------------------->16進位
'[^0-7]' finds any character that is not an octal digit
'%S' is simpler than '[^%s]'.

To count the number of vowels in a text, you can write

_, nvow = string.gsub(text, "[AEIOUaeiou]", "")----------->回傳字母數

print(string.gsub("one, and two; and three", "%a+", "word"))
--> word, word word; word word(一個以上的字母串)
i, j = string.find("the number 1298 is even", "%d+")
    print(i,j)   --> 12  15 (string.find會回傳開頭位置,結束位置)(%d+ --> 一個以上的數字串)
test = "int x; /* x */  int y; /* y */"
    print(string.gsub(test, "/%*.*%*/", "<COMMENT>"))
      --> int x; <COMMENT>
1.這段函示碼的目的是把註解變成<COMMENT>
2.%*這樣寫無效化*的作用
3." . "點這個字元代表任何字元," .* "就變成0個以上的任何字串(可有可無)
4." / "不向反斜線字元,沒有任何作用。
5. int y; /* y */ 這字串被吃掉了,因為點*的關係,讓<COMMENT>取代的目標是 "/* x */  int y; /* y */"
test = "int x; /* x */  int y; /* y */"
    print(string.gsub(test, "/%*.-%*/", "<COMMENT>"))
        --> int x; <COMMENT>  int y; <COMMENT>
將".*"換成".-"的話,就可以"點到為止了"
 
 '[+-]?%d+' matching numerals like "-12""23" and "+1009"
[+-]本來是+或- 多個問號變成有第三個選擇 "無"

if string.find(s, "^%d") then ...
checks whether the string s starts with a digit
抓數字開頭的字串(^的作用,注意不要跟[^0-7]這種互斥用法搞混)
if string.find(s, "^[+-]?%d+$") then ...
checks whether that string represents an integer number, without other leading or trailing characters.
抓整數值的字串,$錢字號在這裡的作用就是以數字結尾,所以不會有小數點連在一起。

Another item in a pattern is the '%b', that matches balanced strings. Such item is written as '%bxy', where x and y are any two distinct characters; the x acts as an opening character and the y as the closing one. For instance, the pattern '%b()' matches parts of the string that start with a `(´ and finish at the respective `)´:

    print(string.gsub("a (enclosed (in) parentheses) line",
                      "%b()", ""))
      --> a  line(目的是把括弧內的字串刪掉)
Typically, this pattern is used as '%b()', '%b[]', '%b%{%}', or '%b<>', but you can use any characters as delimiters.
"%bxy",這裡的x和y是相對應的東西,有左括弧必有右括弧之類的。

The I/O Library

Unlike printwrite adds no extra characters to the output, such as tabs or newlines. Moreover, write uses the current output file, whereas print always uses the standard output. Finally, printautomatically applies tostring to its arguments, so it can also show tables, functions, and nil.

> print("hello", "Lua"); print("Hi")
      --> hello   Lua
      --> Hi
    
> io.write("hello", "Lua"); io.write("Hi", "\n")
      --> helloLuaHi
io.write不加料



arrow
arrow
    全站熱搜

    abcdefg 發表在 痞客邦 留言(0) 人氣()