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

[LUA] Pull information from a string

Started by Raev82, 24 November 2012 - 12:14 PM
Raev82 #1
Posted 24 November 2012 - 01:14 PM
Hello

I have been working on an elevator program with some good success so far. Ive run into a small stumbling block. I dont quite fulling understand the string.match() and string.find() functions, and am unaware if I can add wildcards in strings. Heres some examples of what Im trying to do:

I want to send a signal from one comeputer (on the elevatot) to the main computer (Operating motors and such). The signal would simply say floor 1 or floor 2 etc. The receiving computer takes the statement and matches the string floor for a matching statement. Heres a small exert, but not actual code.

while true do
id, message = rednet.receive()

operation, ammount = message (need to break up the message. The first string is the operation, the number is the ammount)

if operation == "floor" then
elevatorMove(ammount)
end
end


so I have the logical parts taken care of, Im just trying to figure out how, or even if it is possible to break up a string. Also, if there is an easier way, such as putting some sort of separator in such as a | or other that could work as well.

Please advise, and thanks in advance.

Raev82
Lyqyd #2
Posted 24 November 2012 - 01:18 PM
Yep. Give us some example strings and we can come up with a pattern for you, or you could figure out the pattern yourself using the pattern documentation.
Raev82 #3
Posted 24 November 2012 - 01:37 PM
Actually after reading a few posts down, I realized someone asked a question very similar to what I was looking for.

so lets say this is what I was doing.

computer 1 sends a message to computer 2. IE rednet.send(2, floor|1)

computer 1 receives via:
id, message = rednet.receive()

id should then be: 1
message should be: floor|1

Now I should be able to just
operation, amount = string.match(message, "(%w+)|(%w+))

operation should be: floor
amount should be: 1

Does that look right?
Kingdaro #4
Posted 24 November 2012 - 02:29 PM
Except that %w only captures letters. %d captures numbers (digits), so it would be: (%w+)|(%d+)
Lyqyd #5
Posted 24 November 2012 - 03:51 PM
Incorrect, Kingdaro. %a only captures alphabetical characters. %d only captures numerals, and %w captures all alphanumeric characters.
ChunLing #6
Posted 24 November 2012 - 04:19 PM
I sometimes use %S cause that captures everything that isn't a space. But %w is good for most things.
Kingdaro #7
Posted 24 November 2012 - 04:25 PM
Incorrect, Kingdaro. %a only captures alphabetical characters. %d only captures numerals, and %w captures all alphanumeric characters.
Huh. I saw "w" and thought "word". Learn something new every day.