Jump to content




Reading character by character


  • You cannot reply to this topic
5 replies to this topic

#1 SGunner2014

  • Members
  • 113 posts
  • LocationMaidstone, UK

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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#3 SGunner2014

  • Members
  • 113 posts
  • LocationMaidstone, UK

Posted 11 November 2014 - 05:44 PM

Okay, many thanks :)

- Sam

#4 SGunner2014

  • Members
  • 113 posts
  • LocationMaidstone, UK

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 by SGunner2014, 11 November 2014 - 06:35 PM.


#5 valithor

  • Members
  • 1,053 posts

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 by valithor, 11 November 2014 - 09:28 PM.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users