15 posts
Location
France
Posted 09 August 2013 - 06:19 PM
hello,
someone types the command "$$ae show" or "$$ic2 show"
I'm looking to do a function for which definire info display with "ae", "ic2"
sorry for my bad English
--$$ Comand
while true do
local e, msg = os.pullEvent("chat_command")
print(e.." - "..msg)
--if for find ae or ic2
if ("ae") then
print("ae")
elseif ("ic2") then
print("ic2")
end
end
finish part
while true do
local e, msg = os.pullEvent("chat_command")
print(e.." - "..msg)
if string.match(msg, "ae") then
print("ae")
elseif string.match(msg, "ic2") then
print("ic2")
end
end
1190 posts
Location
RHIT
Posted 09 August 2013 - 07:08 PM
You could use
string.match for this. Fortunately you don't really have to use regex if you don't want to (it can be a bit of a learning curve, but it is ultimately worth it in my opinion).
Example:
local chat_message = "$$ae example"
if string.match(chat_message, "^$$ae") then
print("ae")
elseif string.match(chat_message, "^$$ic2") then
print("ic2")
end
The ^ character at the beginning of the match string tells string.match to check only at the beginning of the chat message.
Just a note: I don't think that OpenPeripheral gives you the $$ characters in front of the chat message, even though you have to type it out in the chat. For example, if I type "$$hello", the event returned will be ("chat_message","hello"). I might be wrong, but that's how it works from what I remember.
1604 posts
Posted 09 August 2013 - 07:12 PM
I think the chat message doesn't add the $$ at the start. So, remove the $$ from Bubba's code and it should work with the message you get from the event.
15 posts
Location
France
Posted 09 August 2013 - 07:19 PM
thank you it works very well
15 posts
Location
France
Posted 09 August 2013 - 08:21 PM
how can I do to get the info from the command "$ ae craft May 20"
I want to recover the 5 and 20
how can I make
please
83 posts
Location
Behind you
Posted 10 August 2013 - 06:41 AM
Okay, firstly try to not 'up' like that, but I understand.
Now, all you need to do is separate the chat_message into parts defined by the position of the spaces.
For a much better source than me check out the string documentation!
http://www.lua.org/manual/5.1/manual.html#5.4If there's no better solution, here's one that I can work out.
local mes = chat_message
local arguments = {} -- everything will be stored in a table
while string.find(chat_message,' ') do
table.insert(arguments,string.sub(mes,1,string.find(mes,' ')-1))
mes = string.sub(mes,string.find(mes,' ')+1)
end
table.insert(arguments,mes)
-- just to understand everything
print(arguments[1])
print(arguments[2])
15 posts
Location
France
Posted 10 August 2013 - 07:37 PM
while true do
local e, msg, test = os.pullEvent("chat_command")
print(e.." - "..msg.." - "..test)
local args = {} -- everything will be stored in a table
while string.find(msg,' ') do
table.insert(args,string.sub(msg,1,string.find(msg,' ')-1))
msg = string.sub(msg,string.find(msg,' ')+1)
end
table.insert(args,msg)
-- just to understand everything
print(------------------------])
print("1: "..args[1])
print("2: "..args[2])
print("3: "..args[3])
print("4: "..args[4])
print("5: "..args[5])
end
bug
1583 posts
Location
Germany
Posted 11 August 2013 - 04:43 AM
A very simple seperate function:
local function seperate(txt,sym) --sym means at which symbol you want to seperate
local output = {}
for _,word in string.gmatch(txt, "[^sym]+") do
table.insert(output, word)
end
return output
end
Then you use it like this:
local chat_command = "un deux trois quatre cinq"
local seperated = seperate(chat_command, " ")
for k,v in pairs(seperated) do
print(k, ": ",v)
end
--[[ OUTPUT
1: un
2: deux
3: trois
4: quatre
5. cinq
]]--
1281 posts
Posted 11 August 2013 - 06:18 AM
I've already got an ME bridge runnig on my glasses. Here's what i use to seperate info from the string, modified to work with your code ofcourse
local item = string.match(msg,"craft%s(.+)%s%d+$")
local amount = tonumber(string.match(msg,"%d+$"))
if you pass it the command "$$craft planks 10"
item will be "planks"
and amount will be "10"
Another thing to note about ME bridges, is they use itemIDs, not item names. So you'll have to convert from name to id aswell.
997 posts
Location
Wellington, New Zealand
Posted 11 August 2013 - 08:19 AM
I've already got an ME bridge runnig on my glasses. Here's what i use to seperate info from the string, modified to work with your code ofcourse
local item = string.match(msg,"craft%s(.+)%s%d+$")
local amount = tonumber(string.match(msg,"%d+$"))
if you pass it the command "$$craft planks 10"
item will be "planks"
and amount will be "10"
Another thing to note about ME bridges, is they use itemIDs, not item names. So you'll have to convert from name to id aswell.
And if you pass it the command "$$craft planks 10 50" item will be "planks 10" and amount will be 10, which is unexpected.
If you pass it "$$craft pl4nks 8" item will be "pl4nks" and amount will be 4, which is also unexpected.
string.match can match multiple things like this:
local item, amount = string.match(command, "^craft%s(%S+)%s(%d+)$")
amount = tonumber(amount)
(untested)
7508 posts
Location
Australia
Posted 11 August 2013 - 08:27 AM
Another way is the way that ComputerCraft does it in the Shell program… It is done like so
local tWords = {}
for match in string.gmatch( _sCommand, "[^ \t]+" ) do
table.insert( tWords, match )
end
This will split the command into a table of words, then you would need to go and check each of the words for what you want.
Another thing to note about ME bridges, is they use itemIDs, not item names. So you'll have to convert from name to id aswell.
Also take note that with any item/block with a damage value (metadata) the ID that MiscPeripherals supplies will not be the same as the item/block :(/>
1281 posts
Posted 11 August 2013 - 08:35 AM
I've already got an ME bridge runnig on my glasses. Here's what i use to seperate info from the string, modified to work with your code ofcourse
local item = string.match(msg,"craft%s(.+)%s%d+$")
local amount = tonumber(string.match(msg,"%d+$"))
if you pass it the command "$$craft planks 10"
item will be "planks"
and amount will be "10"
Another thing to note about ME bridges, is they use itemIDs, not item names. So you'll have to convert from name to id aswell.
And if you pass it the command "$$craft planks 10 50" item will be "planks 10" and amount will be 10, which is unexpected.
If you pass it "$$craft pl4nks 8" item will be "pl4nks" and amount will be 4, which is also unexpected.
This is wrong. The $ symbol marks the end of the string. Therefore it would only match the number at the end. Your code would fail on any item with a space however.
As for the damage value ids, there is a workaround. Ill post it later when im at my computer.
1522 posts
Location
The Netherlands
Posted 11 August 2013 - 08:41 AM
A very simple seperate function:
local function seperate(txt,sym) --sym means at which symbol you want to seperate
local output = {}
for _,word in string.gmatch(txt, "[^sym]+") do
table.insert(output, word)
end
return output
end
Then you use it like this:
local chat_command = "un deux trois quatre cinq"
local seperated = seperate(chat_command, " ")
for k,v in pairs(seperated) do
print(k, ": ",v)
end
--[[ OUTPUT
1: un
2: deux
3: trois
4: quatre
5. cinq
]]--
Now your string will split at the s, y or m. Come on, you have been doing this for a while now, you know you need to concat it:
And btw, string.gmatch doesnt return two values 0.0. Did you test this even?
local function seperate(txt,sym) --sym means at which symbol you want to seperate
local output = {}
for word in string.gmatch(txt, "[^" .. sym .. "]+") do
table.insert(output, word)
end
return output
end
1281 posts
Posted 11 August 2013 - 09:14 AM
This code is lifted directly from my glasses program, specifically a part of the itemID input function.
local item = string.match(command,"itemid%s(.+)%s%d+:?%d-$")
local metaId = tonumber(string.match(command,":(/>/>%d+)$")) -- i have no idea what the "/>" is about, i can't get rid of it lol'
local id
if metaId then
id = tonumber(string.match(command,"(%d+):%d+$"))
id = id+(metaId*32768)
else
id = tonumber(string.match(command,"%d+$"))
end
basically what it does is, if it's given a meta id(itemid:metaid) then it takes the metaid, multiplies it by 32786 and adds id on top of that. This is how the ME bridge stores metaIDs. idk why, but if i recall correctly it's a bug. You can find more info about this in the misc peripherals thread, on some of the last few pages.