스트링 배열 정렬(sorting)하기 with Lua
[파일명: testSort.lua]------------------------------------------------
function find(arr, s)
for i = 1, #arr do
if string.find(arr[i], s) ~= nil then
return i
end
end
return -1;
end
function printArray(arr)
io.write("[")
for i = 1, #arr - 1 do
io.write(arr[i] .. ", ")
end
if #arr > 0 then
io.write(arr[#arr])
end
io.write("]\n")
end
words = { "하나", "둘", "셋", "넷", "다섯", "여섯" }
io.write("list: ")
printArray(words)
where = find(words, "셋")
if where > 0 then
io.write("발견! ")
print("Next word of 셋 in list: " .. words[where+1])
end
print("Sorting...")
table.sort(words)
io.write("list: ")
printArray(words)
where = find(words, "셋")
if where > 0 then
io.write("발견! ")
print("Next word of 셋 in list: " .. words[where+1])
end
------------------------------------------------
실행> lua testSort.lua one two three four five
[five, four, one, three, two]
실행> lua testSort.lua 하나 둘 셋 넷 다섯
[넷, 다섯, 둘, 셋, 하나]
실행> lua testSort.lua 자전차 자전거 전동차 전차 전기자동차
[자전거, 자전차, 전기자동차, 전동차, 전차]