Lua Mac&Windows開發環境組建及部分語法例子

Mac:


1.安裝Lua編譯器

  1. 下載Lua, http://www.lua.org/
  2. 解壓後,cd進入該文件夾src目錄下,修改Makefile裏macosx後一行爲 macosx: $(MAKE) all MYCFLAGS="-DLUA_USE_LINUX -arch i386" MYLIBS="-arch i386 -lreadline" 保存退出。
  3. 在當前文件夾執行make macosx   然後回車
  4. cd 到上一目錄,執行sudo make install
  5. 完成之後執行lua -v 可以看到:
    [python]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
    1. Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio  
表示安裝成功;

2.安裝Sublime Text2    

1.下載http://sublime-text-2.cn.uptodown.com後打開;

{  
 "cmd": ["/usr/local/bin/lua", "$file"],  
 "file_regex": "^(...*?):([0-9]*):?([0-9]*)",  
 "selector": "source.lua"  
}  
2.保存爲Lua.sublime-build, 之後重啓Sublime Text 2
3.ctrl+n新建文件,輸入print("hello world")保存到英文路徑目錄下
4.然後ctrl+b啓動編譯。就會看到如圖:

注:一定要保存在英文路徑下,如果保存在中文路徑下會出現如下異常:
[python]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
  1. Traceback (most recent call last):  
  2.   File "./sublime_plugin.py", line 337in run_  
  3.   File "./exec.py", line 130in run  
  4. UnicodeEncodeError: 'ascii' codec can't encode characters in position 36-37: ordinal not in range(128)  

Windows:

下載LuaForWindows安裝包安裝,過程中如果出現無法連接服務器的情況,需要去微軟補丁官網下載Microsoft Visual 2005 SP1的組件安裝,重新安裝即可。安裝成功會出現交互模式(SciTE)和編譯器(Lua)的icon,這樣就可以直接在兩個工具裏面敲寫腳本了:


SciTE需要建個類似文本的文件保存爲後綴名爲XXX.lua,運行需要在上面菜單點擊運行三角圖標:



PS:

學習基礎的部分例子,初識lua,變學習變更新咯

--[[ function fact (n)
if n == 0 then
return 1;
else
return n*fact(n-1)
end
end


print("enter a number:")
a = io.read("*number")       --讀取一個數字
print(fact(a)) ]]--


--[[ line = io.read()
n = tonumber(line)
if n == nil then
error(line .. "is not a valid number")
else
print(n*2)
end ]]--


-- print(tostring(10) == "10")  --> true
-- print(10 .. "" == "10")   --> true


--[[ a = "hello"
print(#a) --> 5 #計算字符串的長度
print(#"good\0bye") --> 8 \0 佔一個字符長度
]]--


--[[
for i = 1,10
do
print(i*10)
end
]]--


--[[
x = math.pi
print(x - x%0.01)
--]]


--[[
a = {}
for i = 1,10
do
a[i] = io.read()
end
local i = 1
while a[i] do
print(a[i])
i = i+1
end
]]--


--[[
for i = 10,1,-2 do
print("for:") print(i)
end
]]--


--[[
--泛型for的使用
a = {4,5,8,10,13,6,9,7}
for i,v in ipairs(a)   -->打印value
do
print(v)
end


for i in pairs(a) -->打印key
do
print(i)
end
print(#a)
]]--


local testTable1 = {"a","b","c","d","e"}
local testTable2 = {1,2,3,4,5}
local testTable3 = {'a','b','c','d','e'}
local testTable4 = {"a","b","c",nil,"e"}
local testTable5 = {'a','b',nil,'d','e'}
local testTable6 = {1,2,nil,4,5}
local testTable7 = {"a","b","c","d",nil}
local testTable8 = {x = "a",y = "b",z = "c"}
local testTable9 = {"1","2","3"}
testTable9[5] = "5"
--testTable9[4] = "4"


print(#testTable1)
print(#testTable2)
print(#testTable3)
print(#testTable4)
print(#testTable5)
print(#testTable6)
print(#testTable7)
print(#testTable8)
print(#testTable9)

--[[
days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
revDays = {}
for k,v in ipairs(days)
do
 revDays[v] = k
end
x = "Tuesday"
print(revDays[x])
]]--




--[[
--函數多值返回情況
function maximum (a)
local mi = 1
local m = a[mi]
for i,v in ipairs(a)
do
if v > m then
mi = i;m = v
end
end
return m,mi
end
print(maximum({8,10,23,12,5}))
]]--




--[[
-- 類似於格式化
print("for" .. 10)


--unpack:返回列表所有值
print(unpack{10,20,30})
]]--


--[[
--傳入可變參數函數例子
function add(...)
local s =0
for i,v in ipairs{...} do
s = s + v
end
return s
end


print(add(3,4,10,25))
]]--

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章