《Programing in Lua》第三部分 CH18-CH19

 

CH18:数学库

  1. 三角:sin, cos, tan, asin, acos (以弧度为单位,可用deg和rad转换角度和弧度)
  2. 指数和对数:exp, log, log10
  3. 取整:floor(向下), ceil(向上)
  4. 最值:max, min
  5. 生成伪随机数:random, randomseed
  6. 变量:pi, huge

 


 

 

CH19:table库

  1. 一个认知点:不能对table的索引进行排序!Lua中table是无序的,而数组的本质是table,当然也是无序的。
  2. 两个问题:
    • table的原始顺序?
    • pairs与ipairs的区别,对于数组为啥必须用ipairs?
  3. 两个难点:
    • 书中table.sort() 迭代器的例子
      tLines = {
      luaH_set = 10,
      luaH_get = 24,
      luaH_present = 48
      }

      function pairsByKey(t, f) --f为可选参数
      local a = {}
      for n in pairs(t) do a[#a + 1] = n end
      table.sort(a, f)
      local i = 0
      return function()
      i = i + 1
      return a[i], t[a[i]]
      end
      end

      for name, line in pairsByKey(tLines) do
      print(name, line)
      end
    • 书中table.concat的一个扩展的例子
      --处理嵌套的字符串数组
      --
      核心:递归
      function rconcat(l)
      if type(l) ~= "table" then return l end
      local res = {}
      for i = 1, #l do
      res[i] = rconcat(l[i])
      end
      return table.concat(res)
      end

      t = { { "a", {" nice"} }, " and", { {" long"}, {" list!"} } }
      print(rconcat(t))
  4. 四个函数
    • table.insert
    • table.remove
    • table.sort
    • table.concat
posted @ 2011-10-17 20:57  nepaul  阅读(587)  评论(0编辑  收藏  举报