Jump to content




[Question] How to read a string as several parts


9 replies to this topic

#1 dark

  • New Members
  • 15 posts

Posted 27 March 2012 - 03: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?

#2 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 27 March 2012 - 03:25 PM

this funciton shoud split the string with the added pattern and return a table note i never tryed it ingame
Spoiler


#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 27 March 2012 - 05: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.

#4 EatenAlive3

  • New Members
  • 53 posts

Posted 27 March 2012 - 07:24 PM

My preferred method is with this very simple function:
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


#5 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 27 March 2012 - 07:39 PM

Just use Tomass' String Utils API. He has a seperate function

#6 dark

  • New Members
  • 15 posts

Posted 27 March 2012 - 08: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.

#7 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 27 March 2012 - 08: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/>

#8 dark

  • New Members
  • 15 posts

Posted 29 March 2012 - 03:32 AM

does anyone know if its possible to read the inventory of a chest and relay it to a computer.

#9 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 29 March 2012 - 04:14 AM

View Postdark, on 29 March 2012 - 03:32 AM, said:

does anyone know if its possible to read the inventory of a chest and relay it to a computer.
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.

#10 dark

  • New Members
  • 15 posts

Posted 29 March 2012 - 10:06 PM

View PostAdvert, on 29 March 2012 - 04:14 AM, said:

View Postdark, on 29 March 2012 - 03:32 AM, said:

does anyone know if its possible to read the inventory of a chest and relay it to a computer.
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.

cool thanks anyways





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users