33 posts
Posted 06 July 2012 - 11:39 AM
I'm trying to create a registration code for my email system that sends the label of the sending computer to a registration server as the variable regname. It also sends it's ID to the server as regid. Now, what I need the server to do is create a variable, except the variable's name needs to be the computer ID's number. I don't know how to create a new variable using the value of another. Any help here?
445 posts
Posted 06 July 2012 - 01:02 PM
Hm probably either a global variable or one in a table
so like
label = "abc"
_G[label] = "bleh"
print(_G["abc"])
or probably better
local labelList = {}
local label = "blah"
labelList[label]="abc"
print(labelList["blah"])
If that makes sense to you
33 posts
Posted 06 July 2012 - 02:10 PM
Sorry to sound like a noob, but I can just BARELY grasp what you typed. And it's not what I need unfortunately; I need to be able to have any computer on the network register with the main server, creating a brand new variable (the name of the computer as determined by sent label) and assigning it the numeric value of the ID the computer that sent it registers as (it appears that the server can see that natively, so disregard the regid send act). I guess you could say I'm trying to make a database, so instead of typing in IDs for the computer targets I can instead type the names, and have the main server look them up and then forward the message to the correct ID by comparing names against the database.
445 posts
Posted 06 July 2012 - 02:13 PM
Well I'm better at helping people who aren't newish xD
And well i just showed a way to use a variable to make another variable named after the former
33 posts
Posted 06 July 2012 - 02:16 PM
Well I'm better at helping people who aren't newish xD
And well i just showed a way to use a variable to make another variable named after the former
If that's what that code does, could you mind pointing out exactly which part does which? If you can explain that to me I could plug this into an array and possibly solve the problem.
I know this creates the table. > local labelList = {}
I'm assuming this creates, inside said table, a variable called "label"? local label = "blah"
What is this? > labelList[label]="abc"
I know this prints it, which is nice.. But I don't really need printback atm. print(labelList["blah"])
445 posts
Posted 06 July 2012 - 02:22 PM
well syntax
table[key] = value
sets the key in table to value
so you have a table which is empty
now it sets the key in this case label which has "blah" as its value (so labelList[label] is the same as doing labelList["blah"] now) to value
so labelList[label]="abc" makes the table:
{
blah="abc"
}
33 posts
Posted 06 July 2012 - 02:37 PM
well syntax
table[key] = value
sets the key in table to value
so you have a table which is empty
now it sets the key in this case label which has "blah" as its value (so labelList[label] is the same as doing labelList["blah"] now) to value
so labelList[label]="abc" makes the table:
{
blah="abc"
}
Is there any way to export the variable in the table now to an external, permanent variable named the same thing? This isn't 100% necessary as I can now accomplish what I needed to, but it would make it a bit easier.
1604 posts
Posted 06 July 2012 - 04:05 PM
You can save it to a file, using the fs api.
If you want to save the whole table, you can serialize it.
local list = {}
-- add something to the list
-- and now save it:
local file = fs.open("path to the save file", "w") -- open the file
if file then -- check if it's open
local s = textutils.serialize(list) -- serialize the table, so we have a string to save to the file
file.write(s) -- write it to the file
file.close() -- always close the file handles
end