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

Database

Started by HunterForce, 24 September 2014 - 05:37 PM
HunterForce #1
Posted 24 September 2014 - 07:37 PM
I was wondering how you would go about creating a search database.

First, off how could you make a table look for a key that matches an io.read input from the user?

Secondly, how could you add a new key/value created in an array based on user input?

Bascially how can you create new tables or keys/ values in a table and how can you then search for them? Will it work with nested tables?



–Thanks in advance for any help! I really appreaciate anything you can suggest!
KingofGamesYami #2
Posted 24 September 2014 - 08:22 PM
To use a variable as a key & search, you can do this:

local var = read()
if tbl[ var ] == "something" then
 --#do stuff
elseif not tbl[ var ] then
  tbl[ var ] = read()
end
HunterForce #3
Posted 25 September 2014 - 03:48 PM
Just for clarification, if var = read() in line 1 then why is it declared again in line 5? Couldnt you just delcare a new table equal to the local var?
KingofGamesYami #4
Posted 25 September 2014 - 04:58 PM
Just for clarification, if var = read() in line 1 then why is it declared again in line 5? Couldnt you just delcare a new table equal to the local var?

What I was doing there, is if the users input exists within the table then do this, else ask for another input and set the first input within the table equal to the second input.

eg:
if tbl is

{ King = "Hello" }
then doing this:

Yami
would prompt again, at which point I could type in

Goodbye
And the table would then look like:

{King = "Hello", Yami = "Goodbye" }