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

Reading character by character

Started by SGunner2014, 11 November 2014 - 04:38 PM
SGunner2014 #1
Posted 11 November 2014 - 05:38 PM
Okay, I'm creating a system where it can save all of the values of the screen colours and text values (i.e. background colour, foreground colour and the text in the pixel). How would I go about reading a string character by character? This would be used to load in a save file and then I've already designed a system that would reproduce it on the screen.

Thanks,
- Sam
Lyqyd #2
Posted 11 November 2014 - 05:43 PM
One way is to just iterate through the string and pull out each character with string.sub:


for i = 1, #str do
  local char = string.sub(str, i, i)
end
SGunner2014 #3
Posted 11 November 2014 - 05:44 PM
Okay, many thanks :)/>

- Sam
SGunner2014 #4
Posted 11 November 2014 - 06:32 PM
Okay, having a few problems.

I want the system to read from a table inside a table. So, I have two for statements that run through all of the x positions for a y value, then moves on to the next y value. And, then, I have your for statement from above to read each character in the set. Currently, the sets of characters are set up as five characters as different values in the y lines sections of the main table. However, when I try to draw a pixel or set the cursor position, it says that the number of the x and y lines it is on (from the first two for statements) aren't numbers. I worked out why this wasn't working, but I can't work out how to get the number of the value of the x and y values it is on. These would be used to draw the pixel and to set the cursor position.

If you understood any of that, could you help me please?

CODE:

function main()
os.loadAPI("test")
for kv, v in pairs(test.cols) do
  for ki, i in pairs(v) do
    if #i == 5 then
      for kt=1, #i do
        local char = string.sub(i,k,k)
        if ki==1 then
          local colour = colFind(char)
 print(ki.." > "..kv)
 --paintutils.drawPixel(tonumber(i),tonumber(v),colour)
        elseif ki==2 then
 local colour = colFind(char)
 --term.setTextColor(colour)
elseif ki==3 then
 --term.setCursorPos(tonumber(i),tonumber(v))
 --print(char)
        end
      end
    end
  end
end
end

Thanks,
- Sam
Edited on 11 November 2014 - 05:35 PM
valithor #5
Posted 11 November 2014 - 09:14 PM
As I understand pairs, it returns the key and value of a table. Because of this I would never be a number (seeing you use it as a string other places in the program), and since we are not provided with the other table I can not say whether or not it is a number. If you wish to get the length of the string you would use #I, or if you wish to see which letter in the string you would use the variable kt.
Edited on 11 November 2014 - 08:28 PM
Bomb Bloke #6
Posted 11 November 2014 - 11:29 PM
Indeed, it's very difficult to comment without seeing all the code you're omitting. Where's this "test" API?