모듈:Coutput
Scribunto 확장에서 사용할 수 없는 표준 루아의 print() 함수와 유사한 기능을 합니다.
사용 방법
편집print() 함수를 사용하면 출력할 내용이 저장됩니다. 프로그램을 마칠 때 마지막으로 printall() 함수를 호출하여 그 결과값을 되돌려 주어야 합니다. 아래와 같은 예제를 참고 바랍니다.
함수
편집- function p.print(o, value)
- o는 table이어야 하며, 이곳에 value 값을 push합니다.
- value는 저장해 둘 값입니다. 자동으로 문자열로 변환되지만 예외적으로 nil 값의 경우 아무것도 저장하지 않습니다.
- function p.printf(o, formatstring, ...)
- o는 table이어야 하며, 이곳에 포맷에 따른 문자열을 push합니다.
- formatstring은 포맷 문자열로서, 이에 따라 이 문자열에 따라서 뒤따라오는 가변 인수들을 포맷합니다.
- ...는 가변 인수들입니다.
- function p.printall(o)
- o는 table이어야하며, 저장되어 있는 모든 문자열을 순서대로 합쳐 리턴합니다.
- function p.example()
- 이 모듈을 사용하는 방법에 관한 예제함수입니다.
일반적 주의 사항
편집- o[1], o[2] 등의 시퀀스는 출력될 문자열을 보관하는 용도로 사용되고 있으므로 다른 용도로 사용하면 오류를 일으킬 수 있습니다.
--출력할 값을 담고 있다가 한번에 출력하는 모듈
local p = {}
function p.print(o, value)
if type(o) ~= "table" then return p end
if type(value) == "nil" then return p end
o[#o+1]=tostring(value) -- 입력받은 대로 하나씩 저장
return p --자기 자신을 리턴
end
function p.printf(o, formatstring, ...)
if type(o) ~= "table" then return p end
--if type(value) == "nil" then return p end
o[#o+1]=string.format(formatstring, ...) -- 입력받은 대로 하나씩 저장
return p --자기 자신을 리턴
end
function p.printall(o)
return table.concat(o, "", 1, #o) -- o 테이블에 저장된 값을 모아서 리턴
end
function p.example()
--[[
local t ={}
p.print(t, "안녕하세요!<br />")
p.print(t, nil)
p.print(t, "1+1=")
p.print(t, 1+1)
return p.printall(t)
]]
p:print("안녕하세요!<br />")
p:print(nil)
p:print("1+1=")
p:print(1+1)
return p:printall()
end
return p