Ah…the unspoken assumption is finally spoken.
Yes, if we assume that the OP is simply wrong about how the sent message should be formatted, and further assume that it is to be used on the other side as separate variables, then it does make a good bit more sense to put them in a table, serialize it, and then unserialize it on the other end.
I will not comment on whether those assumptions are "good". But when you are basing your argument on assumptions not previously stated, it is better to plainly state them.
Yes, I see how that might work… However, I do believe that, given the following clues, the OP intends to use the data separately:
1) "need to send my turtle the deets of what to do" A string containing a bunch of numbers and letters and spaces would be an odd thing for a turtle to act on, without needing to separate the variables.
2) "I could just send the variables in separate rednet.send() messages" If the OP wanted the variables as the string "0 get…", sending the variables separately would make no difference. If he did want the variables as a string, he would still need to concatenate the bits at the other end (which, if this was his intention, he still wouldn't know how to do).
I think the word we were looking for in this thread was "cleaner", as opposed to "efficient".
Because I just can't help defending my code, *ahem*
My code:
--Send
local message = textutils.serialise({id, getput, item, amount})
--Receive
local mT = textutils.unserialise(message)
id = mT[1]; getput = mT[2]; item = mT[3]; amount = mT[4]
The other code:
--Send
local message = id .. " " .. getput .. " " .. item .. " " .. amount
--Receive (string.explode from http://snippets.luacode.org/?p=snippets/stringexplode_90)
function string.explode ( str , seperator , plain )
assert ( type ( seperator ) == "string" and seperator ~= "" , "Invalid seperator (need string of length >= 1)" )
local t , nexti = { } , 1
local pos = 1
while true do
local st , sp = str:find ( seperator , pos , plain )
if not st then break end -- No more seperators found
if pos ~= st then
t [ nexti ] = str:sub ( pos , st - 1 ) -- Attach chars left of current divider
nexti = nexti + 1
end
pos = sp + 1 -- Jump past current divider
end
t [ nexti ] = str:sub ( pos ) -- Attach chars right of last divider
return t
end
local messageTable = string.explode(message, " ", true)
id = mT[1]; getput = mT[2]; item = mT[3]; amount = mT[4]
Now which is cleaner?