LINUX.ORG.RU

История изменений

Исправление LightDiver, (текущая версия) :

function SearchAndSendToGuild(str, array)
    if not str or not array then return end
    for i=1, #array do
        if string.find(array[i]:lower(), str:lower()) then
            SendChatMessage(array[i], "GUILD")
        end
    end
end
function SearchAndSendToGuild(searchStr, array)
    if SendChatMessage then
        if not searchStr or searchStr == "" then
            print("Ошибка: пустая подстрока для поиска")
            return
        end
        
        if not array or type(array) ~= "table" then
            print("Ошибка: неверный массив")
            return
        end
        
        local found = {}
        local pattern = ".*" .. searchStr:gsub("%W", "%%%1") .. ".*"
        
        for i, item in ipairs(array) do
            if type(item) == "string" then
                if string.match(string.lower(item), string.lower(pattern)) then
                    table.insert(found, item)
                end
            end
        end
        
        if #found == 0 then
            SendChatMessage("По запросу '" .. searchStr .. "' ничего не найдено.", "GUILD")
        else
            SendChatMessage("Результаты поиска '" .. searchStr .. "' (" .. #found .. "):", "GUILD")
            for i, item in ipairs(found) do
                if i <= 10 then
                    SendChatMessage(i .. ". " .. item, "GUILD")
                end
            end
            if #found > 10 then
                SendChatMessage("... и еще " .. (#found - 10) .. " совпадений", "GUILD")
            end
        end
    else
        print("Ошибка: функция SendChatMessage не найдена")
    end
end

Еще не самый плохой вариант, что я видел. Можно добавить еще кучу бесполезных проверок на то, что точно существует, вынести подфункции из функции, желательно куда нибудь подальше. Сделать нелинейную логику, чтобы прилось помотаться по коду туда сюда. А почему нет то, и правда.

Исходная версия LightDiver, :

function SearchAndSendToGuild(str, array)
    if not str or not array then return end
    for i=1, #array do
        if string.find(array[i]:lower(), str:lower()) then
            SendChatMessage(array[i], "GUILD")
        end
    end
end
function SearchAndSendToGuild(searchStr, array)
    if SendChatMessage then
        if not searchStr or searchStr == "" then
            print("Ошибка: пустая подстрока для поиска")
            return
        end
        
        if not array or type(array) ~= "table" then
            print("Ошибка: неверный массив")
            return
        end
        
        local found = {}
        local pattern = ".*" .. searchStr:gsub("%W", "%%%1") .. ".*"
        
        for i, item in ipairs(array) do
            if type(item) == "string" then
                if string.match(string.lower(item), string.lower(pattern)) then
                    table.insert(found, item)
                end
            end
        end
        
        if #found == 0 then
            SendChatMessage("По запросу '" .. searchStr .. "' ничего не найдено.", "GUILD")
        else
            SendChatMessage("Результаты поиска '" .. searchStr .. "' (" .. #found .. "):", "GUILD")
            for i, item in ipairs(found) do
                if i <= 10 then
                    SendChatMessage(i .. ". " .. item, "GUILD")
                end
            end
            if #found > 10 then
                SendChatMessage("... и еще " .. (#found - 10) .. " совпадений", "GUILD")
            end
        end
    else
        print("Ошибка: функция SendChatMessage не найдена")
    end
end