I am creating an API/Program that takes a file reads it, creates a raw table of every line it read and creates empty tables with names inside the raw table. For example if raw[14] equaled "Commands" I want it to generate a table with that as the name. How can I achieve this? I thank anybody who can help. =)
2
4 replies to this topic
#1
Posted 24 July 2016 - 05:05 PM
#2
Posted 24 July 2016 - 05:32 PM
If you set the variable "line" (without quotes) to the value of the line, do:
_G[line] = {}
Because _G is a table representing all the global variables
_G[line] = {}
Because _G is a table representing all the global variables
#3
Posted 24 July 2016 - 06:48 PM
powerboat9, on 24 July 2016 - 05:32 PM, said:
If you set the variable "line" (without quotes) to the value of the line, do:
_G[line] = {}
Because _G is a table representing all the global variables
_G[line] = {}
Because _G is a table representing all the global variables
Edited by MagicCraftMaster, 24 July 2016 - 06:50 PM.
#4
Posted 24 July 2016 - 06:54 PM
It sounds like some of the error message is getting lost. Here's my two cents on the original problem:
- Yes, you can make a table in _G named "Commands". But what is the purpose of that table? And how / why will you access it later?
- Putting any variables in _G is bad practice, as they persist until reboot. What if one of the lines was "os", and you later wanted to use a function in the os API? You can't, because you overwrite the pointer. Instead, add it to your own table, local to your program.
- Yes, you can make a table in _G named "Commands". But what is the purpose of that table? And how / why will you access it later?
- Putting any variables in _G is bad practice, as they persist until reboot. What if one of the lines was "os", and you later wanted to use a function in the os API? You can't, because you overwrite the pointer. Instead, add it to your own table, local to your program.
local tStuff = {} tStuff[ raw[ 14 ] ] = {}
#5
Posted 24 July 2016 - 06:55 PM
I think you want to return the table like this:
local dataFile = <API name here>.<API function name here>(...)
If you want to return a directory, you can save the name of the files as the keys of the table:
{
startup = {...}
[newFile] = {...}
}
You can add the files like this:
I think you want to return the table like this:
local dataFile = <API name here>.<API function name here>(...)
If you want to return a directory, you can save the name of the files as the keys of the table:
{
startup = {...}
[newFile] = {...}
}
You can add the files like this:
<API name here>.<API function name here> = function(...) <CODE> return res endSo you don't need to set the name. You can use it like this:
local dataFile = <API name here>.<API function name here>(...)
If you want to return a directory, you can save the name of the files as the keys of the table:
{
startup = {...}
[newFile] = {...}
}
You can add the files like this:
<API name here>.<API function name here> = function(...) <get the files of the directory and iterate them> for ... do ... directory[name] = data ... end ... return directory end
I think you want to return the table like this:
<API name here>.<API function name here> = function(...) <CODE> return res endSo you don't need to set the name. You can use it like this:
local dataFile = <API name here>.<API function name here>(...)
If you want to return a directory, you can save the name of the files as the keys of the table:
{
startup = {...}
[newFile] = {...}
}
You can add the files like this:
<API name here>.<API function name here> = function(...) <get the files of the directory and iterate them> for ... do ... directory[name] = data ... end ... return directory end
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users