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

[LUA] String to array help

Started by gamesaucer, 14 July 2012 - 09:06 AM
gamesaucer #1
Posted 14 July 2012 - 11:06 AM
I am trying to pass a list of available folders via rednet as a string. The message is successfully received, as a print points out: both the server (sends the list) and the client (receives the list) share the same information.

First, the server compiles the data into a string:
Folders: {"lol", "omfg", "test"}
String to send: "lol#omfg#test#"
Client receives: "lol#omfg#test#"

I try to make it into a list using this code here:

function makeArray(str)
s = str
local i = 1
local returnvalue = {}
while string.find(s,"#") do
  returnvalue[i] = string.sub(s,string.find(s,"#")-1)
  s = string.sub(s,string.find(s,"#")+1)
  i = i + 1
end
return returnvalue
end

The result is not
{"lol", "omfg", "test"}
as I expected, but
{"lol","omfg#tes","#"}
instead.

What am I doing wrong here?
AfterLifeLochie #2
Posted 14 July 2012 - 11:09 AM
If you're talking about a split function, this is what I use:

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

Where pString would be "lol#omfg#test#" and pPattern would be "#"
gamesaucer #3
Posted 14 July 2012 - 11:43 AM
Thank you, it worked. :P/>/>
I'm still trying to find out why my own code didn't work, though.
AfterLifeLochie #4
Posted 14 July 2012 - 11:46 AM
Thank you, it worked. :P/>/>
I'm still trying to find out why my own code didn't work, though.

You're welcome. I'll have a look at your code again and mess around with it, and see if I can get it working for you.
AfterLifeLochie #5
Posted 14 July 2012 - 11:57 AM
I found the problem: string.sub() has a couple of missing parameters that you need to make it work as intended. The documentation at http://lua-users.org/wiki/StringLibraryTutorial demonstrates that string.sub() takes the following parameters:

string.sub(s, i [, j])

s, being the string, i being the starting point and j being the end (inclusive). So in order to make your program work, I adjusted lines 6 and 7:


function makeArray(str)
s = str
local i = 1
local returnvalue = {}
while string.find(s,"#") do
  returnvalue[i] = string.sub(s, 0, string.find(s,"#")-1)
  s = string.sub(s, string.find(s, "#") + 1, string.len(s))
  i = i + 1
end
return returnvalue
end
-- Example:
r = makeArray("lol#omfg#test#")
for i = 1,3 do
print(tostring(i) .. ": " .. r[i])
end
eddieo #6
Posted 14 July 2012 - 04:07 PM
it is easy if you use the textutils.serialize and textutils.unserialize api
to save a table to a file


local t= {alpha,beta,charlie,delta,echo) -- a simple table
local ts = textutils.serialize( t )-- converte the table to a string
local file = fs.open("abc.txt" , "a") -- opens a file or creates a file in append mode
file.writeLine(ts) -- writes the string to the file
file.close()-- closes the file

to extract the string from a file


file = fs.open("abc.txt" , "r") -- opens file
local line = file.readLine() -- reads a line from the file
t = textutils.unserialize(line) -- converts the string back into a table
file.close() -- closes the file
print(t[1]) -- prints alpha
print(t[2]) -- prints beta
print(t[3]) -- prints charlie
print(t[4]) -- prints delta
print(t[5]) -- prints echo

by changing the code to send it over rednet this may work for you convert it to a string and send it and have who ever recieves it convert it back