1
votes

I'm learning Lua and probably don't have a great grasp on how the language works but I'm trying to create a split function for the string library:

string.split = function(str, delimiter)

    -- A function which splits a string by a delimiter
    values = {}
    currentValue = ""
    for i = 1, string.len(str) do

        local character = string.sub(str, i, i)
        if(character == delimiter) then

            table.insert(values,currentValue)
            currentValue = ""

        else

            currentValue = currentValue..character

        end

    end

    -- clean up last item
    table.insert(values,currentValue)

    return vaules

end

values is not nil if I print it out before the return, but if I call t = string.split("hello world", " "), t will be nil. I'm not quite sure why my table is disappearing

1

1 Answers

2
votes

You have a typo in your return statement.

vaules

Instead of values.

vaules is nil of course.

Another advice: make variables local wherever possible.