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

Some help with something with strings

Started by santa22, 14 January 2014 - 04:31 PM
santa22 #1
Posted 14 January 2014 - 05:31 PM
Hey, i been working with computercraft for a while, but recently i wanted to try something

I tried to find a way to have a turtle read single digits from a string.
Like this

"1000001"
I want it to read the first digit which is 1, then do a action, then read the next which is 0. and so on
I currently use computercraft for minecraft 1.5.2 right now. so if anyone has any ideas plz help.
Bomb Bloke #2
Posted 14 January 2014 - 08:37 PM
string.sub() is what you're after here. Eg:

local myString = "1000001"

for i=1,#myString do   -- Loop once for every character in the string.
  local myCharacter = string.sub(myString,i,i)  -- Get the next character from the string.

  if myCharacter == "0" then
    -- Do something.
  elseif myCharacter == "1" then
    -- Do something else.
  end
end

http://lua-users.org/wiki/StringLibraryTutorial
santa22 #3
Posted 14 January 2014 - 11:37 PM
Thanks thats exactly what i needed.
I wont use it exactly, but ill use it as a base for what i'm doing.
robhol #4
Posted 15 January 2014 - 03:03 AM
or, you can do:

for char in str:gmatch(".") do
...
end