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

String help needed D:

Started by Senmori, 18 March 2013 - 12:53 PM
Senmori #1
Posted 18 March 2013 - 01:53 PM
Problem Solved.


My plan was to make a script using MiscPeripherals' ChatBox to refuel my turtles to a certain level via chat.
Yes. I'm that lazy…

My problem isn't catching the input or even adding commas for the sake of my OCD. My problem is I can't figure out how to newline after each set of numbers.

Example:
Chat: quarry 40000
Returns: quarry 40,000

However I want to catch multiple input on the same chat line. Thus making me even lazier.
Example:
Chat: quarry 40000 xquarry 4000 ….
should return:
quarry 40,000
xquarry 40,000

Except it doesn't. It gives me an 'attempt to index ? (a nil value)' error.
So I guess my question is how would I split the string after the numbers and interpret each one?

Here's the code:
Spoiler

function comma(num)
	 local a,b = string.match(num, '^([^%d]*%d)(%d*)$')
	 return a..(b:reverse():gsub('%d%d%d', '%1,'):reverse())
end


while true do
	 local e, player, text = os.pullEvent("chat")
		 print("Player: "..tostring(player))
		 print("Text: "..comma(text))
end

Pastebin: http://pastebin.com/mpT4jJ7v
SuicidalSTDz #2
Posted 18 March 2013 - 01:58 PM
Have fun! I've never used chat boxes and never plan on it..
Senmori #3
Posted 18 March 2013 - 02:01 PM
D: I love being lazy though!
Kingdaro #4
Posted 18 March 2013 - 02:09 PM
Use gmatch.


local input = 'quarry 40000 xquarry 4000'

for cmd, param in input:gmatch('(%S+)%s+(%S+)') do
  print(cmd..' '..comma(param))
end
Senmori #5
Posted 18 March 2013 - 02:20 PM
<_</>

Thank Kingdaro.

Problem solved.

<3