33 posts
Location
127.0.0.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…
864 posts
Location
Sometime.
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
445 posts
Posted 23 July 2012 - 11:45 AM
I'd prefer a split function for this
864 posts
Location
Sometime.
Posted 23 July 2012 - 11:47 AM
string.match is a text/number splitter based on patterns.
445 posts
Posted 23 July 2012 - 11:47 AM
It can be used as that, usually its quite inflexible for that though
864 posts
Location
Sometime.
Posted 23 July 2012 - 11:48 AM
Yes, but it allows for 1 string.
445 posts
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
864 posts
Location
Sometime.
Posted 23 July 2012 - 11:51 AM
-.- I'm just saying.
8543 posts
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.
445 posts
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 :)/>/>
33 posts
Location
127.0.0.1
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?
864 posts
Location
Sometime.
Posted 24 July 2012 - 08:13 AM
It returns a table (serialize).
33 posts
Location
127.0.0.1
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]
864 posts
Location
Sometime.
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 :)/>/>
33 posts
Location
127.0.0.1
Posted 24 July 2012 - 08:30 AM
Tested it, and it works! Thank you!
864 posts
Location
Sometime.
Posted 24 July 2012 - 08:31 AM
HAHAH! Thanks for telling me it worked, now I can finish my program..