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

Deleting a letter from a string

Started by milkywave1, 25 November 2012 - 02:56 AM
milkywave1 #1
Posted 25 November 2012 - 03:56 AM
I need to send an input from a computer to another computer, and it needs to use this:

rednet.send(10, input.."q")
(It's done like this right? :lol:/>/> )
BUT, when the other computer receives it, I need to do some maths do the input, and I don't know how to remove the "q"from the string(So that I can convert it into a number). Please help me :D/>/> . I'm a beginner at Lua so… yea :P/>/> Help will be much appreciated! :D/>/>
Doyle3694 #2
Posted 25 November 2012 - 04:02 AM

sender, message = rednet.receive()
sentnumber = string.sub(message,#message-1,#message)
message = string.sub(message,1,#message-1)
Kingdaro #3
Posted 25 November 2012 - 04:06 AM
Some other solutions, you could capture the number with match:

id, message = rednet.receive()
number = tonumber(message:match('(%d+)q'))
if number then
  -- stuff
end

Or you could simply remove the q altogether with gsub:

id, message = rednet.receive()
number = tonumber(message:gsub('q',''))
if number then
  -- stuff
end
billysback #4
Posted 25 November 2012 - 04:10 AM
or you could do:

local id, message, distance = rednet.receive()
number = tonumber(message:sub(1, -2))
Lyqyd #5
Posted 25 November 2012 - 02:12 PM
Or, since you're only appending the q to make it into a string:


rednet.send(10, tostring(input))

--receive:
id, message = rednet.receive()
message = tonumber(message)
Kingdaro #6
Posted 25 November 2012 - 02:16 PM
Isn't it turned into a string when it's sent, though? I haven't actually sent a number though rednet before, so I'm not sure. :/
Lyqyd #7
Posted 25 November 2012 - 02:24 PM
No, it'll yell at you if you try passing non-strings as the message.
ChunLing #8
Posted 26 November 2012 - 07:35 AM
I was trying to think of a plausible explanation for why the q was needed in the first place.
Kingdaro #9
Posted 26 November 2012 - 07:39 AM
I was trying to think of a plausible explanation for why the q was needed in the first place.
I thought it was a check to make sure that a specific receiving program only process the data with the "q" at the end, and not other rednet signals.