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

Tables Or Storing Multiple Variables

Started by techno9487, 14 August 2013 - 02:42 AM
techno9487 #1
Posted 14 August 2013 - 04:42 AM
Introduction
In Computer craft and lua in general there is a way of storing more than one data type in one variable. For example:


local myTable = {}

This declares a table but you can replace "myTable" with any name you want

To be able to store values in a table you need to use an index:


local myTable = {test="lol",num=3}

print(myTable["test"])
print(tonumber(myTable["num"]))

The first line of code is making a table called myTable and adding two variables inside "test" and "num" then the two print commands print out the value of myTable[test]and myTable[num]

To add/read variables from a table you type


myTable[<index name="">] = "value to be stored"

or to receive


print(myTable["index of value to be retrieved "])

That's it for now but more chapters will be coming later click the link to see how to use tables with rednet to send nultiple messages in one ! "http://www.computerc...c/14743-relays/</index>

Examples

Example #1, storing and retrieving passwords


Spoiler

local passwords = {}

function main()
term.clear()
term.setCursorPos(1,1)

print("Enter new user:")
userNew = read()

print("Enter password: ")
passNew = read("x")

passwords[userNew] = passNew

print("Do you want to view users? y/n")
ans = read()

if ans == "y" then
  term.clear()
  term.setCursorPos(1,1)

  print("Enter user: ")
  userFind = read()

  print("Password for user '"userFind"' is '"..passwords[userFind]"'")
  read()
end
end

while true do
main()
end

Troubleshooting

I don't understand!
In this case please post bellow the issue you are having and I will try to help you

It's giving me an error!

If this happens do the following:

    *Create a reply to this topic *In the reply add the code you are using and the error that is being generated
again I will try to help you the best I can
eriksters1 #2
Posted 29 September 2013 - 07:50 AM
1. in " print(tonumber(myTable["num"])) '' what does the '' tonumber '' do?
2.why do u even need the num?
3. myTable[<index name="">] = "value to be stored" i have no freaking idea what does this do!
4. (im a newbie!)
5.print(myTable["index of value to be retrieved "]) i also have no idea what this does!
LBPHacker #3
Posted 10 October 2013 - 12:41 AM
-snip-
Why did nobody answer him?! Fixing that :D/>
  1. tonumber turns strings into numbers. "1" becomes 1, "3.1415926535897932" becomes 3.14159265 (yeah, only 8 decimals). It returns nothing if the string cannot be converted into a number. But, for the record, using tonumber in that example was pointless, since myTable["num"] is a number anyways.
  2. The "num" key of myTable contains a number, as you can see that in the first line.
  3. The forum software messed that up. Would be something like myTable["key"] = "value to be stored".
  4. Not a problem.
  5. Prints the "index of value to be retrieved" key of myTable.
jay5476 #4
Posted 10 October 2013 - 04:55 AM

local myTable = {}

This declares a table but you can replace "myTable" with any name you want

To be able to store values in a table you need to use an index:
you do not need to use an index, but everything in the table will be indexed and by default index's will be numeric
mrgreaper #5
Posted 26 November 2013 - 07:27 PM
cant get my head around this… ok in lua

able = {"m","o","p"}
print (able[1])

result
m
1


so how do i lose the second line? that i can do alpha = able[1] and have alpha as m and not the double line one?
Lyqyd #6
Posted 26 November 2013 - 08:05 PM
The second line is print returning the number of lines printed. If you just type in able[1] instead of print(able[1]), it'll just show m.
Neekow #7
Posted 30 November 2013 - 08:50 AM
things would be cool to be implemented in your tutorial:
- subtable (how to write, insert, read etc …)
- how to wrap a function

i say that because i took a very long time to find how it works ^^ (when you'r not a coder and english isnt your native langage, its pretty hard)
apemanzilla #8
Posted 02 December 2013 - 09:09 AM
It also may be worth explaining the system of keys and values a bit more in-depth, and possibly the dot notation (table.key)