70 posts
Posted 30 July 2013 - 04:17 AM
Hey Coder's
I have a problem for my new program, because I dosn't found a way to do that what I want. Maybe one of you can code it.
I have a few variables they are all standing for a item ID. So maybe they are going so L1 to L64, M1 to M64 and R1 to R64. So now I will look with a function in what variable my item is. So I will type the number and found the Variable with the number. How can I make the function? Hope someone can help me.
Thanks a lot,
Timia2109
P.S sorry for bad English :/
587 posts
Location
Wrocław, Poland
Posted 30 July 2013 - 07:06 AM
You should store all your variables in table to make is easy (it's possible to store those in variables, but this is advanced Lua).
Example
Spoiler
-- Create table
local items = {}
--Let's put stone ID at index "stone"
items['stone'] = 1
--And torch at 50
items['torch']= 50
-- Searching function
local function search(table, toSearch)
-- Check if given table is actually table
if type(table) ~= "table" then
-- If it's not, raise error
error("Table expected, got "..type(table) or "nil", 2)
end
-- Loop trough all values in given table
-- Set "index" to current item's index and set "value" to current item's value
for index, value in pairs(table) do
-- If we fount it
if index == toSearch then
-- Return fount value
return value
end
end
-- If we reached this point it meas that items was not fount
-- Return false since nothing was fount
return false
end
-- Some testing
-- Search for "stone" in table "items"
print(tostring(search(items, "stone"))) --> 1
print(tostring(search(items, "noSuchThing"))) --> false
-- Search inside nil
search(nil, "stone")
Better syntax highlighting: pastebin
70 posts
Posted 30 July 2013 - 10:36 AM
Ok a big thank's to you! I think I have understand it!
But there is question that have got now. How can I save and load tables in a file? How I can do that with a variable I know, but how can I make it so?
Thanks again,
Timia2109
587 posts
Location
Wrocław, Poland
Posted 30 July 2013 - 10:37 AM
Cool! :D/>
1852 posts
Location
Sweden
Posted 30 July 2013 - 01:30 PM
Ok a big thank's to you! I think I have understand it!
But there is question that have got now. How can I save and load tables in a file? How I can do that with a variable I know, but how can I make it so?
Thanks again,
Timia2109
Would this be useful? .-.
nTable = {}
-- Writing table to file
function wTable(filename,nTable)
file = fs.open(filename,"w")
file.writeLine(textutils.serialize(nTable)
file.close()
end
-- Reading tables from files
function rTable(filename)
file = fs.open(filename,"r")
nTable = textutils.unserialize(file.readAll())
file.close()
return nTable
end
1190 posts
Location
RHIT
Posted 30 July 2013 - 01:41 PM
-snip-
This works perfectly well and is simple. However, we can actually speed the search up quite a bit using the indexes.
For example:
local tItems = {
["torch"] = 50;
["stone"] = 1;
["cobblestone"] = 4;
}
local function search(item)
return tItems[item]
end
print(search("cobblestone")) --#Prints out 4
print(search("torch")) --#Prints out 5
print(search("batman")) --#Prints out nothing because batman is not an item
587 posts
Location
Wrocław, Poland
Posted 30 July 2013 - 01:52 PM
Hell blazes, how could I not think about it :P/>
70 posts
Posted 30 July 2013 - 03:19 PM
A big thanks goes to you all! I don't know the table function until now. I be so happy, that I now can make my program now. Thanks a lot to you all!
1522 posts
Location
The Netherlands
Posted 30 July 2013 - 06:31 PM
You can also do it with this function, but I highly suggest to use a table.
local function getVar( varName )
if type( varName ) ~= "string" then error( "String expected, got " .. type( varName ), 2 ) end
local env = getfenv( 1 )
if env[varName] then
return env[varName]
end
return nil
end
x = 5
print( getVar("x") )
However with this way you cannot declare your variables locally.
587 posts
Location
Wrocław, Poland
Posted 30 July 2013 - 06:43 PM
instead of env use _G
1522 posts
Location
The Netherlands
Posted 30 July 2013 - 06:51 PM
instead of env use _G
The variable doesnt get into the _G table. Only in the environment of the program itself.
587 posts
Location
Wrocław, Poland
Posted 30 July 2013 - 07:12 PM
Oh? Didn't knew that :P/>
70 posts
Posted 03 August 2013 - 11:31 AM
Hey Guys,
thank you for show me the table function it's so helpful, but a question again.
Today I found this
Spoiler
local FileList = fs.list("")
for _, file in ipairs(FileList) do
print(file)
end
and now the question. How can I take from the table, the count of the files and how can I read one of the elements like file1, file2…?
Thanks,
timia2109
1190 posts
Location
RHIT
Posted 03 August 2013 - 01:12 PM
-snip-
So essentially you want a way to count the number of files in a folder?
Pretty simple, just use the # operator on the table returned by fs.list
Example:
local fileList = fs.list("/rom/programs")
print("The number of files in rom/programs is: " .. #fileList)
To get the names of the files, all you have to do is use a loop.
Example:
local fileList = fs.list("/rom/programs")
for index, fileName in ipairs(fileList) do
print("File: " .. fileName)
end
This would print out the name of every file/folder inside of rom/programs.