This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Question] How to read a string as several parts
Started by dark, 27 March 2012 - 02:07 PMPosted 27 March 2012 - 04:07 PM
i'm wanting to program a complex assembly line controller and need to send it a lot of numbers, settings, and so on. But i don't want to send one thing at a time since the program can confuse that as something different. I recall that in Java you can read a string and break it up into small words and numbers. For example it would read a string like ("order:6:32:47:Generators:137:JImmy") this would be broken down into Order Time product amount ordering-Person. This way the program can then uses those small parts too restrict what needs done. But, with my small experience as a coder i actual don't know how to do this. I know how to set up the rest just not that part so help would be lovely. Also is there a way to detect the contents of a chest?
Posted 27 March 2012 - 04:25 PM
this funciton shoud split the string with the added pattern and return a table note i never tryed it ingame
local Table = {}
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
Spoiler
function split(pString, pPattern)local Table = {}
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
Posted 27 March 2012 - 06:15 PM
You could also send a serialized table containing the values:
function sendOrder(order, time, product, amount, to)
local t = {}
t["Order"] = order
t["Time"] = time
t["Product"] = product
t["Amount"] = amount
t["To"] = to
local msg = textutils.serialize(t)
rednet.send(msg, id)
end
sendOrder("order", os.time(), "Generators", 137, "Jimmy")
And then receive it like this:
function getOrder()
local id, msg = rednet.receive()
return textutils.unserialize(msg)
end
local t = getOrder()
print("Order: ", t["Order"])
print("Product: ", t["Product"])
print("Amount: ", t["Amount"])
You should add some checks to see if the message is an order, but it should work.Posted 27 March 2012 - 08:24 PM
My preferred method is with this very simple function:
Of course, this is noticeably repeated. You can shorten it into a function, or just use this one, which uses a comma as a seperator:
It returns a table, so assign x = getSeperated(aString) and use x[1], x[2], etc to access the values. Since it called the pattern function, you have to include that in the same file.
For what you need, here is a modified version:
function pattern(text,pattern,start)
return string.sub(text,string.find(text,pattern,start)) end
Example usage:
string = "this,is,an,example,5"
count = 0
Q = pattern(string,"[^,]+",count)
count = count + Q:len() + 1
W = pattern(string,"[^,]+",count)
count = count + W:len() + 1
E = pattern(string,"[^,]+",count)
count = count + E:len() + 1
R = pattern(string,"[^,]+",count)
count = count + R:len() + 1
T = pattern(string,"[^,]+",count)
count = count + T:len() + 1
--To convert the 5 in string to the number 5, just do:
T = tonumber(T)
Of course, this is noticeably repeated. You can shorten it into a function, or just use this one, which uses a comma as a seperator:
function getSeperated(string)
local sep = {}
local done = false
local count,prevVars,tmp = 0,0,0
string = string..",|"
while not done do
count = count + 1
tmp = pattern(string,"[^,]+",count+prevVars)
if tmp == "|" then done = true return sep end
prevVars = prevVars + tmp:len()
table.insert(sep,count,tmp)
end
return sep
end
It returns a table, so assign x = getSeperated(aString) and use x[1], x[2], etc to access the values. Since it called the pattern function, you have to include that in the same file.
For what you need, here is a modified version:
Spoiler
function pattern(text,pattern,start)
return string.sub(text,string.find(text,pattern,start)) end
function getSeperated(string)
local sep = {}
local done = false
local count,prevVars,tmp = 0,0,0
string = string..":|"
while not done do
count = count + 1
tmp = pattern(string,"[^:]+",count+prevVars)
if tmp == "|" then done = true return sep end
prevVars = prevVars + tmp:len()
table.insert(sep,count,tmp)
end
return sep
end
Posted 27 March 2012 - 08:39 PM
Just use Tomass' String Utils API. He has a seperate function
Posted 27 March 2012 - 09:26 PM
wow thank you, didn't think it would be that easy to code. IF i use your code right i can use it for anything or use that api. Either way thank you for helping my noobness.
Posted 27 March 2012 - 09:44 PM
Do you mean me?
If answer == "yes" then
print("Your welcome")
print("Just ask me if you want to know something. Via PM for example")
else
print("Ignore me")
end
:o/>/>
If answer == "yes" then
print("Your welcome")
print("Just ask me if you want to know something. Via PM for example")
else
print("Ignore me")
end
:o/>/>
Posted 29 March 2012 - 04:32 AM
does anyone know if its possible to read the inventory of a chest and relay it to a computer.
Posted 29 March 2012 - 05:14 AM
That's not possible in CC. You'll have to use some peripheral (mod), but I don't know if any that do this are released yet.does anyone know if its possible to read the inventory of a chest and relay it to a computer.
Posted 29 March 2012 - 11:06 PM
That's not possible in CC. You'll have to use some peripheral (mod), but I don't know if any that do this are released yet.does anyone know if its possible to read the inventory of a chest and relay it to a computer.
cool thanks anyways