user1569088
user1569088

Reputation: 21

How do you separate a string and/or number into an array?

I'm working with the language Lua, and I have a long string of binary numbers, which I'd like to put into an array with each digit as its own element within the array.

Upvotes: 2

Views: 278

Answers (1)

furq
furq

Reputation: 5788

local str, out = "01101101010010010", {}
for i = 1, #str do
  out[#out+1] = string.sub(str,i,i)
end

Change line 3 to out[#out+1] = tonumber(string.sub(str,i,i)) if you want the array to contain numbers rather than strings.

Upvotes: 3

Related Questions