일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- C++ API
- 중소규모택지
- lua install
- 전동포 #송파구전기자전거 #전동킥보드수리 #모토벨로대리점 #전기자전거판매 #전동스쿠터수리 #배터리수리 #전기자전거수리 #송파구전동킥보드 #전동이동수단
- #서초구맛집 #교대역된장찌개 #옥된장교대점 #서초된장맛집 #교대직장인맛집 #된장찌개전문점 #서울된장정식 #혼밥맛집 #건강한한끼 #교대점심맛집
- Lua
- 엑스퍼트생일축하해
- 육전국밥 #강남역맛집 #24시간맛집 #소고기국밥 #육전 #모둠전 #해물파전 #강남한식 #혼밥맛집 #강남역국밥
- file read
- 수도권주택공급
- file write
- object
- lua setup
- QTcpServer
- lua for windows
- C API
- 국토교통부
- 찾다죽는줄
- QT TCP
- FILE TRANSFER
- TCP/IP
- 월세
- lua interpreter
- #신혼부부 #결혼준비 #신혼부부희망타운신혼부부특별공급
- 티몬삼겹살데이
- 등록임대주택
- #부동산전자거래 #부동산전자계약 #부동산계약 #부동산전자계약방법 #부동산전자계약하는법 #부동산계약방법 #부동산중개수수료 #부동산중개수수료아끼기 #부동산복비아끼기
- 엑스퍼트2주년
- 편편집 #강남역맛집 #강남샤브샤브 #강남무한리필 #편백찜맛집 #월남쌈맛집 #샤브샤브맛집 #가성비맛집 #강남회식 #강남데이트
- 순남시래기 #교대맛집 #교대역맛집 #한식맛집 #시래기국 #서울맛집 #가성비맛집 #건강한식단 #셀프반찬 #직장인맛집
- Today
- Total
Value Creator의 IT(프로그래밍 / 전자제품)
#3 Lua Tutorial ( File 열기, 쓰기 / 파일호출 / table 및 metatable) 본문
#3 Lua Tutorial ( File 열기, 쓰기 / 파일호출 / table 및 metatable)
valuecreatort 2019. 4. 17. 19:211. File Open
[소스코드]
--file open
-- r : Read only (default)
-- w : overwrite or create a new file(덮어쓰기)
-- a : Append or create a new file (추가하기)
-- r+ : Read & Write existing file
-- w+ : Overwrite read or create a file
-- a+ : Append read or create file
file = io.open("testr2.lua", "w+")
file:write("Random String of text\n")
file:write("some more text\n")
file:seek("set", 0)
print(file:read("*a"))
file:close()
file = io.open("tex2t.lua","a+")
file:write("Evenmore text\n")
file:seek("set",0)
print(file:read("*a"))
file:close()
[결과]
Random String of text
some more text
Evenmore text
Evenmore text
Evenmore text
Evenmore text
아래 그림처럼 파일이 새로 생긴다.
2. 파일 호출 Convert
[소스코드]
[main.lua] 파일
local function main()
convertModule = require("Convert")
print(string.format("%.3f cm", convertModule.ftToCm(12)))
end
main()
[Convert.lua] 파일
local convert = {}
function convert.ftToCm(feet)
return feet + 30.48
end
return convert
같은 프로젝트에 넣어놓으면 다른 파일을 불러올 수 있다.
[결과]
42.480 cm
3. Table
[소스코드]
[main.lua]
local function main()
aTable = {}
for x = 1, 10 do
aTable[x] = x
end
mt = {
__add = function(table1, table2)
sumTable = {}
for y = 1, #table1 do
if(table1[y] ~= nil) and (table2[y] ~= nil) then
sumTable[y] = table1[y] + table2[y]
else
sumTable[y] = 0
end
end
return sumTable
end,
__eq = function(table1, table2)
return table1.value == table2.value
end,
__lt = function(table1, table2)
return table1.value < table2.value
end,
__le = function(table1, table2)
return table1.value <= table2.value
end,
}
setmetatable(aTable, mt)
print(aTable == aTable)
addTable = {}
addTable = aTable + aTable
for z = 1, #addTable do
print(addTable[z])
end
end
main()
[결과1]
true
2
4
6
8
10
12
14
16
18
20
[소스코드2]
[metatable.lua]
Animal = { height = 0, weight = 0, name = "No name", sound = "No sound"}
function Animal:new (height, weight, name, sound)
setmetatable({}, Animal)
self.height = height
self.weight = weight
self.name = name
self.sound = sound
return self
end
function Animal:toString()
animalStr = string.format("%s weighs %.1f lbs, is %.1f in tall and says %s",self.name, self.weight, self.height, self.sound)
return animalStr
end
spot = Animal:new(10, 15, "Spot", "Woof")
print(spot.weight)
print(spot:toString())
Cat = Animal:new()
function Cat:new(height, weight, name, sound, favFood)
setmetatable({}, Cat)
self.height = height
self.weight = weight
self.name = name
self.sound = sound
self.favFood = favFood
return self
end
function Cat:toString()
animalStr = string.format("%s weighs %.1f lbs, is %.1f in tall and says %s and love %s",self.name, self.weight, self.height, self.sound, self.favFood)
return animalStr
end
caca = Cat:new(22, 33, "caca", "yaong","fish")
print(caca.weight)
print(caca:toString())
[결과]
15
Spot weighs 15.0 lbs, is 10.0 in tall and says Woof
33
caca weighs 33.0 lbs, is 22.0 in tall and says yaong and love fish
참조 사이트 : https://www.youtube.com/watch?v=iMacxZQMPXs
'1. 프로그래밍 > 2) LUA' 카테고리의 다른 글
#6 [lua 1.1] 개요 - Lua 내부 코드 읽어보기 (0) | 2019.08.21 |
---|---|
#5. [LUA] Lua 설치하기(Lua For Windows) (0) | 2019.04.19 |
#4. [LUA] C 또는 C++ 과 Lua script연동 (0) | 2019.04.18 |
#2 Lua 모든 문법 핵심 요약 (0) | 2019.04.12 |
#1 Lua 프로그래밍 Chap 0. Lua Development Tools 활용(특징 : Eclipse 기반, 자동 완성기능) (0) | 2019.03.19 |