lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Oct 24, 2007, at 06:08, Patrick Donnelly wrote:

Would it be possible to have a fifth optional parameter to
table.concat to have it use a passed function on values that are not
strings or numbers, to return strings.

Sure... roll your own... e.g.:

local function concat( anEncoder, aTable, aSeparator, aStart, anEnd )
    local aStart = aStart or 1
    local anEnd = anEnd or #aTable
    local aBuffer = {}

    for anIndex = aStart, anEnd do
        aBuffer[ #aBuffer + 1 ] = anEncoder( aTable[ anIndex ] )
    end

    return table.concat( aBuffer, aSeparator, aStart, anEnd )
end

local t = { 'hi', 5, nil, 4, function() end, {} }

print( concat( tostring, t, ' ', 1, #t ) )

> hi 5 nil 4 function: 0x103da0 table: 0x103f40