Variable Search
Started by timia2109, Jul 30 2013 03:17 AM
13 replies to this topic
#1
Posted 30 July 2013 - 03: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 :/
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 :/
#3
Posted 30 July 2013 - 09: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
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
#4
Posted 30 July 2013 - 09:37 AM
Cool!
#5
Posted 30 July 2013 - 12:30 PM
timia2109, on 30 July 2013 - 09:36 AM, said:
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
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
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
#6
Posted 30 July 2013 - 12:41 PM
jesusthekiller, on 30 July 2013 - 06:06 AM, said:
-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
#7
Posted 30 July 2013 - 12:52 PM
Hell blazes, how could I not think about it
#8
Posted 30 July 2013 - 02: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!
#9
Posted 30 July 2013 - 05: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.
#10
Posted 30 July 2013 - 05:43 PM
instead of env use _G
#12
Posted 30 July 2013 - 06:12 PM
Oh? Didn't knew that
#13
Posted 03 August 2013 - 10:31 AM
Hey Guys,
thank you for show me the table function it's so helpful, but a question again.
Today I found this
Thanks,
timia2109
thank you for show me the table function it's so helpful, but a question again.
Today I found this
Spoiler
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
#14
Posted 03 August 2013 - 12:12 PM
timia2109, on 03 August 2013 - 10:31 AM, said:
-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.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











