Notes/Redis
LUA 스크립트
꿀잠마스터
2025. 7. 24. 22:50
기본
-- 출력
print(123)
print('Hello World')
print("Hello World")
-- 변수 local 선언
local sumLocal = 1 + 1
print(sumLocal)
-- 변수 global 선언
sumGlobal = 2 + 2
print(sumGlobal)
-- if-then-end
local a = 1
if a > 0 then
print("a 는 0보다 크다")
end
if a ~= 0 then -- not 비교
print("a는 0이 아니다")
end
if a == 1 then -- 동일 비교
print("a는 1이다")
end
if 0 and '' then
print("0과 빈문자열은 참이다")
end
if false or not true then
print("이 출력문은 표시되지 않는다")
end
배열
-- 배열
local colors = {'red','green','blue'}
-- lua의 배열 인덱스는 "1"부터 시작
print(colors[1])
-- 요소의 수
print(#colors)
-- 추가
table.insert(colors, 'orange')
print(colors[4])
print(#colors)
-- 배열 순회
for i, v in ipairs(colors) do
print(i, v)
end
-- 범위 순회
for i=5, 10 do
print(i)
end
테이블
-- lua table은 javascript 의 object 와 유사하다
local user = {id = 'a1', name = 'samantha'}
print(user['id'])
print(user['name'])
for k, v in pairs(user) do
print(k, v)
end