Created
May 3, 2014 01:41
-
-
Save motoishmz/d19367376c391c0981c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- this | |
-- is | |
-- a | |
-- single | |
-- line | |
-- comment | |
--[[ | |
this | |
is | |
a | |
multiline | |
comment | |
]] | |
print("hello lua") | |
print("====================") | |
--[[ | |
[variable type] | |
- num | |
- string | |
- boolean (!!!: false, nilだけが偽、それ以外はすべて真。 0もtrueになる) | |
- nil | |
- table | |
- function | |
note: | |
- 型は動的に決まる | |
]] | |
our_valiable = "yo" | |
print(our_valiable) | |
-- !!!: イケてる 多重代入可能 | |
x, y, z = 10, 20, 30 | |
print(x) | |
print(y) | |
print(z) | |
x, y = y, x -- swap | |
print(x) | |
print(y) | |
print("====================") | |
--[[ | |
strings | |
]] | |
s = "our string" | |
print(s) | |
s = "this is a\ttab" | |
print(s) | |
s = [[this is a\ttab]] | |
print(s) | |
s = "hello " .. "lua" -- !!!:結合は .. | |
print(s) | |
s = "hello " .. 123 -- !!!:結合は .. | |
print(s) | |
print("---") | |
s = "hello i am a sample text" | |
print( "len:" .. string.len(s) ) | |
print( "#:" .. #s ) | |
print( "find 'a':" .. string.find(s, "i") ) | |
print( "sub '0, 5':" .. string.sub(s, 0, 5) ) | |
print( "gsub 'hello, hi':" .. string.gsub(s, "hello", "hi") ) | |
print("====================") | |
--[[ | |
table (hash) | |
]] | |
a = {23, 456, "hi"} | |
print(a[0]) -- !!!: nil luaの配列は 1 origin | |
print(a[1]) -- 23 | |
print(a[2]) -- 456 | |
print(a[3]) -- hi | |
user = { name = "motoi", score = 10000 } | |
print(user["name"]) | |
print(user.score) | |
print("====================") | |
--[[ | |
control structure::if | |
]] | |
if user.score > 1000 then | |
print("dragon!") | |
elseif user.score > 500 then | |
print("yolo!") | |
else | |
print("sigh.....") | |
end | |
-- 関係演算子 | |
if user.name == "motoi" then | |
print("username is motoi") | |
end | |
if user.name ~= "shimizu" then | |
print("not equalは != じゃなくて ~= なので注意") | |
end | |
-- 論理演算子 !!!: and not or | |
our_num = 50 | |
our_bool = true | |
if our_num > 30 and our_num < 60 then | |
if our_bool and true then | |
print("and not or") | |
end | |
end | |
print("====================") | |
--[[ | |
control structure::loop | |
for | |
while | |
repeat...until (初回は必ず実行される) | |
]] | |
i = 0 | |
for i = 0, 9 do -- 0~9 | |
print(i) | |
end | |
print("---") | |
i = 0 | |
for i = 0, 9, 2 do -- !!!: 0~9, 2ずつincする | |
print(i) | |
end | |
print("---") | |
pnd = {"poems", "not", "demos"} | |
for i, value in ipairs(pnd) do | |
print(i, value) | |
end | |
print("---") | |
p = {oldschool = "fatboy slim", newschool = "run dmc"} | |
for key, value in pairs(p) do | |
print(key, value) | |
end | |
print("---") | |
i = 0 | |
while i < 10 do | |
print(i) | |
i = i + 1 -- i += 1 はダメ | |
end | |
print("---") | |
i = 0 | |
repeat | |
print(i) | |
i = i + 1 -- i += 1 はダメ | |
until i >= 10 | |
print("---") | |
print("====================") | |
--[[ | |
function | |
]] | |
function greet(name) | |
return "Greetings, " .. name | |
end | |
print( greet("Norman Cook") ) | |
function sum(...) -- !!!: 引数何個でも | |
local numbers = {...} | |
local total = 0 | |
for i = 1, #numbers do | |
total = total + numbers[i] | |
end | |
return total | |
end | |
print( "sum(1,2,3,4,5,6,7,8,9,10): " .. sum(1,2,3,4,5,6,7,8,9,10) ) | |
print("====================") | |
--[[ | |
date | |
]] | |
print( os.date() ) | |
print( os.date("%Y-%m-%d") ) | |
print("---") | |
date = os.date("*t") | |
for key, value in pairs(date) do | |
print(key, value) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment