[LUA] Chap. 2 Type and Value

[예제]
--#string replace
a= "one string"
b= string.gsub(a,"one","another")
print(a)
print(b)
--#string length
a = 'hello'
print(#a)
print(#"good\0bye")
print("good\0bye")
--# '' and "" both OK
print("a 'line'")
print('ano"the"r line')
--#escape
print("one line\nnext line\n\"in quotes\", 'in quotes'")
print('a backslash insed quotes: \'\\\'')
print("a simpler way: '\\'")
--#long string, == or === or ==== or ....
page = [==[
<html>
</html>
]==]
print(page)
--#force conversion
print("10" + 1)
print("10 + 1")
print("-5.3e-10"*"2") -- force convert from string to number
--print("hello" + 1) --error
print(10 .. 20)
print(5)
--#convert from string to number
line = io.read() --input random value, read one line
n = tonumber(line)
if n == nil then
  error(line .. " is not a valid number")
else
  print(n*2)
end
print(tostring(10) == "10")
print(10 .. "" == "10")

--table is object
a = {}
k = "x"
a[k] = 10
a[20] = "great"
print(a["x"])
k = 20
a["x"] = a["x"] + 1
print(a["x"])
a["y"] = 10
b = a
print(b["y"])
b["y"] = 20
print(a["y"])
a = nil
b = nil
c={}
for i=1, 1000 do c[i]=i*2 end
print(c[9])
c["x"] = 10
print(c["x"])
print(c["y"])
c.x = 30
print(c.x)
print(c.y)
d ={}
x = "y"
d[x] = 10
print(d[x])
print(d["y"])
print(d[y])
print(d.x)
print(d.y)
a = {}
for i = 1, 10 do
  a[i] = io.read()
end
for i = 1, #a do
  print(a[i])
end















[결과]
one string
another string
5
8
good bye
a 'line'
ano"the"r line
one line
next line
"in quotes", 'in quotes'
a backslash insed quotes: '\'
a simpler way: '\'
<html>
</html>
11
10 + 1
-1.06e-009
1020
5
4
8
true
true
10
11
10
20
18
10
nil
30
nil
10
10
nil
nil
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10























[예제]

--type(nil) == nil
print(.0e12)
--print(.e12)
print(127/10)
xmlex = [====[
<![CDATA[
  Hello world
]] >
]====]
print(xmlex)
xmlex5 = [==[
<![CDATA[
  Hello world
]] >
]==]
print(xmlex5)
a = {}; a.a = a
print(a)
print(a.a)
print(a[a])
a.a.a.a = 3
print(a)
print(a.a)
--print(a.a.a) -- error
--print(a.a.a.a) --error





[결과]
0
12.7
<![CDATA[
  Hello world
]] >
<![CDATA[
  Hello world
]] >
table: 000000000057F240
table: 000000000057F240
nil
table: 000000000057F240
3


[LUA] Chap. 2 Type and Value [LUA] Chap. 2 Type and Value Reviewed by kukanuc on 3월 06, 2019 Rating: 5

댓글 없음:

Powered by Blogger.