diff options
-rw-r--r-- | conversions.lua | 7 | ||||
-rw-r--r-- | file.lua | 10 | ||||
-rw-r--r-- | function.lua | 24 | ||||
-rw-r--r-- | hello.lua | 1 | ||||
-rw-r--r-- | iter.lua | 11 | ||||
-rw-r--r-- | m.lua | 11 | ||||
-rw-r--r-- | module.lua | 9 | ||||
-rw-r--r-- | scope.lua | 20 | ||||
-rw-r--r-- | scrap.lua | 4 | ||||
-rw-r--r-- | table.lua | 20 |
10 files changed, 117 insertions, 0 deletions
diff --git a/conversions.lua b/conversions.lua new file mode 100644 index 0000000..04a3d70 --- /dev/null +++ b/conversions.lua @@ -0,0 +1,7 @@ +-- conversions --
+
+c = 0xdeadbeef
+h = tonumber(c)
+
+print(h)
+
diff --git a/file.lua b/file.lua new file mode 100644 index 0000000..6e55b3f --- /dev/null +++ b/file.lua @@ -0,0 +1,10 @@ +-- writing to a file --
+fd = io.open("my.txt", "a")
+io.output(fd)
+io.write("a hello message from Lua, bro!\n")
+io.close()
+
+for line in io.lines("my.txt") do
+ print(line)
+end
+
diff --git a/function.lua b/function.lua new file mode 100644 index 0000000..e46da78 --- /dev/null +++ b/function.lua @@ -0,0 +1,24 @@ +-- simple function --
+function checkAge(t)
+ if ((t.age and "he entered valid age") == nil) then
+ print("no age")
+ else
+ print("he entered his age")
+ end
+end
+
+function printDict(t, flag)
+ for k, v in pairs(t) do
+ if (flag ~= nil) then
+ print(k)
+ else
+ print(k, v)
+ end
+ end
+end
+
+t1 = { name = "Kyle", ["age"] = 22, ["my status"] = "amused" }
+print("checking age... ")
+checkAge(t1)
+printDict(t1, nil)
+
diff --git a/hello.lua b/hello.lua new file mode 100644 index 0000000..50de868 --- /dev/null +++ b/hello.lua @@ -0,0 +1 @@ +print("Hello " .. ",world")
\ No newline at end of file diff --git a/iter.lua b/iter.lua new file mode 100644 index 0000000..8a3bd01 --- /dev/null +++ b/iter.lua @@ -0,0 +1,11 @@ +function square(state, n) + if n < state then + n = n + 1 + return n, n * n + end +end + +for i, n in square,42,0 do + print(i, n) +end + @@ -0,0 +1,11 @@ +module(..., package.seeall);
+
+function printDict(t)
+ if not (t) then
+ return
+ end
+ for k, v in pairs(t) do
+ print(k, v)
+ end
+end
+
diff --git a/module.lua b/module.lua new file mode 100644 index 0000000..c18bb3c --- /dev/null +++ b/module.lua @@ -0,0 +1,9 @@ +-- module? --
+
+require "m"
+t = { name = "Ron Paul", origin = "Texas", title = "President"}
+m.printDict(t)
+
+-- don't know the parameter of getfenv --
+print(m.printDict(getfenv(1)))
+
diff --git a/scope.lua b/scope.lua new file mode 100644 index 0000000..eecd804 --- /dev/null +++ b/scope.lua @@ -0,0 +1,20 @@ +-- scope --
+
+do
+ -- variable y not in global scope anymore --
+ local y = 21
+end
+
+function foo()
+ -- on the first foo call, the x below will be defined globally --
+ -- oy oy oy ooooooy --
+ x = 13;
+
+ -- y is not seen here --
+ print(y)
+end
+
+print(x) -- no x defined --
+foo()
+print(x) -- x defined after call to foo --
+
diff --git a/scrap.lua b/scrap.lua new file mode 100644 index 0000000..1eb6846 --- /dev/null +++ b/scrap.lua @@ -0,0 +1,4 @@ +-- scrap --
+t = { apple = "green", orange = "orange", banana = "yellow", strawberry = "red" }
+table.foreach(t, print)
+
diff --git a/table.lua b/table.lua new file mode 100644 index 0000000..d3ef7ba --- /dev/null +++ b/table.lua @@ -0,0 +1,20 @@ +-- print array of values --
+t0 = { 1,3,7,12,13,21,33,42 }
+t0["oops"] = "what will happen" -- nothing --
+for i in ipairs(t0) do
+ print(t0[i])
+end
+
+-- print t0 array backwards --
+-- the for loop, initialization, condition, step --
+for i = #t0, 1, -1 do
+ print(t0[i])
+end
+
+-- print array of string where a key is of type string (dictionary) --
+t1 = { name = "Kyle", ["age"] = 22, ["my status"] = "amused" }
+
+for k, v in pairs(t1) do
+ print(k,v)
+end
+
|