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

[Lua][Question] arrays over rednet

Started by maddigger00009, 30 September 2012 - 02:10 PM
maddigger00009 #1
Posted 30 September 2012 - 04:10 PM
can you send an array over rednet? if not can i some how send one as a string.
inventor2514 #2
Posted 30 September 2012 - 04:19 PM
You can convert a table to a string using textutils.serialize( table )
jag #3
Posted 30 September 2012 - 04:21 PM
If you send the words separated by a space you could do
local array = {}
local id, message = rednet.receive()
for word in string.gmatch(message, "%a+") do
  table.insert(array, word)
end
jag #4
Posted 30 September 2012 - 04:28 PM
You can convert a table to a string using textutils.serialize( table )
And like inventor2514 mean, you can split the array/table when you send it
local array = {
  "chicken",
  "car",
  "craftComputer"
}
local sendString = ""

for _, value in pairs(array) do
  sendString = sendString + " " + value
end

string.sub(sendString, 2) -- This just removes the first 'space'

rednet.send(compId, sendString)
-- This will send:
-- chicken car craftComputer