Jump to content




Removing Text From A String


4 replies to this topic

#1 MayContainVennom

  • Members
  • 47 posts

Posted 29 August 2013 - 06:30 PM

Hello there,

I am wanting to write a code that uses the ME bridge with the chat box that will let me pull items from my ME system by listening to chat. The problem I am having is that if you are not within a 64 block radius the chatbox can't use the tell function.( I'm on a SMP server) So the way I can see to solve this is to have the request and the item Id on the same line. So what I need to do is separate the calling command from the block ID within in variable is this possible?

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 29 August 2013 - 07:26 PM

You mean something like this?

local command, id = string.match(chatInput, "(%a+) (.*)")


#3 MayContainVennom

  • Members
  • 47 posts

Posted 30 August 2013 - 07:32 AM

Well the input would be somthing like !request 1 example. I need to take the!request out so the ME peripheral can pull out ID 1.

#4 floppyjack

  • Members
  • 18 posts

Posted 30 August 2013 - 07:59 AM

I suggest you to read the 'String Manipulation' chapter of the Lua 5.1 Reference Manual:
http://www.lua.org/m...manual.html#5.4
Play around with the functions shown there and you should be able to come up with a solution to your problem.

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 30 August 2013 - 08:03 AM

Well firstly, if you wish to solve the range problem you can do 1 of 2 things.

1. If you have access to the SMP configs, bump up the range
2. Use OpenPeripheral, the terminal have unlimited range and can work cross-dimension too.

Now to the actual problem:

It is completely possible to parse a command and get the args from chat, firstly I would do this:


Using the method that Lyqyd posted we can extract the first word and the args like so
local cmd, args = chatInput:match("(%a+) (.+)")

Now this will actually parse all chat messages, which if not coded correctly, can become problematic with the computer. So we have it detect the ! at the start, if that doesn't exist, then it will return nil
local cmd, args = chatInput:match("!(%a+) (.+)")

if cmd then
  --# we now have a valid format of request through chat
end

Now we can also build a database of commands with a table like so
local commands = {
  ["request"] = doRequest,
  ["other"] = doOther,
  ["another"] = doAnother,
}

Now the format is the command (must be one word) is the key for the table and a function pointer as the value. A function pointer if you don't know is the address to the location in memory that a function is stored, so for example
function test()
  print("hello world")
end
is a function, and using `test` would get us the function pointer, using `print(test)` you can see this location in memory as "table: [location]" but we are straying from topic, so lets bring it back.

We then must define the function
local function doRequest( args )
  --# process the args and perform the request
end

now we should also check that the users command was actually a valid one, for this we do the following
if commands[cmd] then
  --# it was in the table, it is valid
else
  --# invalid command
end

So if we put together what we have so far we get the following

local function doRequest( args )
  --# process the args and perform the request
end


local commands = {
  ["request"] = doRequest,
}

local cmd, args = chatInput:match("!(%a+) (.+)")

if cmd then
  if commands[cmd] then
    commands[cmd]() --# call the function pointer, we have been given th
  else
    --# alert user that it was an invalid command
  end
--# no else here, it wasn't a command for us
end

As for in the `doRequest` function, there are several methods of attack, this is the one I like to use
local function doRequest(args)
  local qty, name = args:match("(%d+) (%a+)")
  if not (qty and name) then
    --# alert the user that they have mistyped the command and it must be "!request [qty] [name]"
    return
  end
  --# valid command, process the request here
end

Now all you need to do is put the process of gathering and processing the command into a loop, actually listen for the command, and you're set to having a program that can respond to commands.

A great tutorial on Patterns in Lua and how they work can be found in the Tutorials section and also the PIL.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users