Jump to content




Tables Or Storing Multiple Variables


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

#1 techno9487

  • Members
  • 14 posts

Posted 14 August 2013 - 03: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

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

#2 eriksters1

  • New Members
  • 1 posts

Posted 29 September 2013 - 06: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!

#3 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 09 October 2013 - 11:41 PM

View Posteriksters1, on 29 September 2013 - 06:50 AM, said:

-snip-
Why did nobody answer him?! Fixing that :D
  • 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.
  • The "num" key of myTable contains a number, as you can see that in the first line.
  • The forum software messed that up. Would be something like myTable["key"] = "value to be stored".
  • Not a problem.
  • Prints the "index of value to be retrieved" key of myTable.


#4 jay5476

  • Members
  • 289 posts

Posted 10 October 2013 - 03:55 AM

View Posttechno9487, on 14 August 2013 - 03:42 AM, said:



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

#5 mrgreaper

  • Members
  • 88 posts

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?

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#7 Neekow

  • Members
  • 55 posts

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)

#8 apemanzilla

  • Members
  • 1,421 posts

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)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users