Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Immutable String Buffers

Name: Anonymous 2011-09-17 22:28

Why is this telling me to concatenate small strings to the ends of large ones, but not in reverse, and then concatenate all remaining strings in the table?
http://www.lua.org/pil/11.6.html

Wouldn't it just be easier to put all the strings in a table then concatenate them all at once, as in the following?

function filetostring(file)
    local t == {}
    for line in io.lines(file)
        if line == "" then
            break
        end
        table.insert(t, line .. '\n')
   return table.concatenate(x)
end

I'm confused as fuck.

Name: Anonymous 2011-09-17 22:38

>>4
If I have to concatenate every line together and I'm doing it line by line why not just do it at once.

I can see why this is wrong.
 -- WARNING: bad code ahead!!
    local buff = ""
    for line in io.lines() do
    buff = buff .. line .. "\n"
    end

But how is this any better then what I wrote? If you need to allocate a new piece of memory every time you assign a string to a variable, then why wouldn't this be worse?
function addString (stack, s)
    table.insert(stack, s)    -- push 's' into the the stack
    for i=table.getn(stack)-1, 1, -1 do
        if string.len(stack[i]) > string.len(stack[i+1]) then
            break
        end
        stack[i] = stack[i] .. table.remove(stack)
    end
end

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List