[파일명:  testStringFindInList.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 testStringFindInList.lua
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in list: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

Posted by Scripter
,