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

[SOLVED] String to table?

Started by Glotz659, 23 July 2012 - 08:18 AM
Glotz659 #1
Posted 23 July 2012 - 10:18 AM
Hi guys!
I just wanted to ask you how to put a string into a table using spaces or commas as seperators. I wrote a program to send commands over rednet
(http://www.computerc...remote-control/, check it out!) and i'm trying to protect that feature with a password so it's impossible for griefers to delete all of your data without entering your base. I would prefer the syntax to be like

rednet.send(receiverID, "Password command")
or

rednet.send(receiverID, "Password, command")


and sorry if it's bad english but it's not my native language…
Noodle #2
Posted 23 July 2012 - 11:02 AM
string.match(msg, "PATTERN HERE")
That would be the way.
EXAMPLE:
while true do
   id, msg = rednet.receive()
   password, message = string.match(msg, "(%a+), (%a+)")
   if password == "blah" then
       hFile = fs.open("blah", "w")
       hFile.write(message)
       hFile.close()
   end
end
-- THIS IS ONLY EXAMPLE
Pinkishu #3
Posted 23 July 2012 - 11:45 AM
I'd prefer a split function for this
Noodle #4
Posted 23 July 2012 - 11:47 AM
string.match is a text/number splitter based on patterns.
Pinkishu #5
Posted 23 July 2012 - 11:47 AM
It can be used as that, usually its quite inflexible for that though
Noodle #6
Posted 23 July 2012 - 11:48 AM
Yes, but it allows for 1 string.
Pinkishu #7
Posted 23 July 2012 - 11:50 AM
? Well yeah, but say you want to stick a third on that or what if some messages have 3 arguments and some 2
Noodle #8
Posted 23 July 2012 - 11:51 AM
-.- I'm just saying.
Lyqyd #9
Posted 23 July 2012 - 02:23 PM
? Well yeah, but say you want to stick a third on that or what if some messages have 3 arguments and some 2

Then you'd just use a string.gmatch iterator. The textutils API is probably what OPis looking for. Specifically, the serialize and unserialize functions.
Pinkishu #10
Posted 23 July 2012 - 02:48 PM
? Well yeah, but say you want to stick a third on that or what if some messages have 3 arguments and some 2

Then you'd just use a string.gmatch iterator. The textutils API is probably what OPis looking for. Specifically, the serialize and unserialize functions.

Thats a possibility :)/>/>
Glotz659 #11
Posted 24 July 2012 - 08:07 AM
Thank you for helping me! I curtrently use the string.match, but as Pinkishu said, it'll only work with two arguments.
The textutils API is probably what OPis looking for. Specifically, the serialize and unserialize functions.
I opend a lua prompt ant tried it, but it won't return anything. i tried

a, b, c = textutils.unserialize("d, e, f")
but it didn't return anything. then i tried to return a, b and c manually, but they were nil.

Could you please tell me the proper syntax?
Noodle #12
Posted 24 July 2012 - 08:13 AM
It returns a table (serialize).
Glotz659 #13
Posted 24 July 2012 - 08:15 AM
so it's
table = {textutils.serialize("a, b, c")}
?

EDIT: no it isnt :)/>/>. it just returns "a, b, c" as table[1]
Noodle #14
Posted 24 July 2012 - 08:17 AM
Hmm.. I'll get looking at the syntax and get back to you on how you would code that.. :o/>/>
EDIT: I see now, it returns a string of the table containing the info and unserialize just takes that info and puts it back into a table.
Example (Un-tested):

-- Send
tet = {}
a = textutils.serialize(tet)
rednet.send(id, a)

-- Recieve
-- After received (id, msg)
a = textutils.unserialize(msg)
for i = 1,#a do
print(a[i])
end
Something like that :)/>/>
Glotz659 #15
Posted 24 July 2012 - 08:30 AM
Tested it, and it works! Thank you!
Noodle #16
Posted 24 July 2012 - 08:31 AM
HAHAH! Thanks for telling me it worked, now I can finish my program..