Jump to content




Variable Search


  • You cannot reply to this topic
13 replies to this topic

#1 timia2109

  • Members
  • 71 posts

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 :/

#2 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 30 July 2013 - 06: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

Better syntax highlighting: pastebin

#3 timia2109

  • Members
  • 71 posts

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

#4 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 30 July 2013 - 09:37 AM

Cool! :D

#5 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 30 July 2013 - 12:30 PM

View Posttimia2109, 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
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


#6 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 30 July 2013 - 12:41 PM

View Postjesusthekiller, 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 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 30 July 2013 - 12:52 PM

Hell blazes, how could I not think about it :P

#8 timia2109

  • Members
  • 71 posts

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 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 30 July 2013 - 05:43 PM

instead of env use _G

#11 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 30 July 2013 - 05:51 PM

View Postjesusthekiller, on 30 July 2013 - 05:43 PM, said:

instead of env use _G
The variable doesnt get into the _G table. Only in the environment of the program itself.

#12 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 30 July 2013 - 06:12 PM

Oh? Didn't knew that :P

#13 timia2109

  • Members
  • 71 posts

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
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 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 03 August 2013 - 12:12 PM

View Posttimia2109, 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