This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Evil_Bengt's profile picture

Dividing strings

Started by Evil_Bengt, 05 October 2014 - 05:05 PM
Evil_Bengt #1
Posted 05 October 2014 - 07:05 PM
Maybe there already is a tread about this but yeah…
Is there any way to split a string into individual characters?
Like, could I split a var with the value: "jmfs" or "8254" to 4 vars with j, m, f and s as values? or maybe 8254 as a number to 4 vars with 8, 2, 5, 4 as values? Thanks in advance…
TheOddByte #2
Posted 05 October 2014 - 07:15 PM
You mean something like this?

local function split( str )
    local chars = {}
    for i = 1, #str do
        table.insert( chars, str:sub( i, i ) )
    end
    return unpack( chars )
end

local a, b, c, d = split( "1234" )
print( a ) -- > 1
print( b ) -- > 2
print( c ) -- > 3
print( d ) -- > 4
Evil_Bengt #3
Posted 05 October 2014 - 07:33 PM
Exactly! Thanks alot! :D/>