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

[LUA][Question]

Started by TCGM, 06 July 2012 - 09:39 AM
TCGM #1
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?
Pinkishu #2
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
TCGM #3
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.
Pinkishu #4
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
TCGM #5
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"])
Pinkishu #6
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"
}
TCGM #7
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.
MysticT #8
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