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

help on tables and 2 other things :3

Started by cheekycharlie101, 06 November 2012 - 08:55 AM
cheekycharlie101 #1
Posted 06 November 2012 - 09:55 AM
ok, so there are some things i really cant understand in computercraft. my question here is could you explain the following things further. i have seen all of these used A LOT in code. so here are the things i cant understand. could you tell me what each one does and how it can be used in code?
thanks -Cheeky.


1. Tables

i do not understand how tables can be used at all. i know how to create one like the following.
local options = {
"option1",
"option2",
}
but i do not know how this can be used in code. please can someone explain.

the next one is tonumber()
i do not know anything about this and do not understand it at all =/

example:
the function
tonumber()



and the last one is inpairs. im not sure if this is a function or what but ive seen it been used in for loops a lot. so like this

for k,v inpairs do
  -code
end

i have no idea what inpairs means or does. please can someone help me out on these?:P/>/>
thanks -Cheeky
Luanub #2
Posted 06 November 2012 - 10:25 AM
1: Table elements are stored as indices. You can you access them by using tableName[indicieValue].

Example:

local tTable = {"example1","example2"}
print(tTable[1])
print(tTable[2])

You can create custom indices. See this tutorial its has a small section on tables. There is alot you can do with them. There are also known as array's if your familiar with other languages. http://www.computerc...not-have-known/

2: tonumber() will convert a string into a number so you can do something like.

write("Enter a number: ")
local input = tonumber(read())

And the users input will be a number and not a string. This will help you when doing math fuctions or even having the user say enter the computer ID to have rednet send a message to.

3: ipairs interates through standard numbered indicies in tables, This is being depreciated in future versions of Lua so there is not much use getting to familiar with it.
Example

for var, x in ipairs(tTable) do
  print(tTable[x])
end

--this is how you will have to do it in the future
for x=1, #tTable do
  print(tTable[x])
end

pairs will take you through non-standard indices

local tTable = {}
tTable[name1] = "Luanub"
tTable[name2] = "Cheeky"

for var, x in pairs(tTable) do
  print("The names in the table are "..tTable[x])
end

You should probably go through the tutorials here on the forums and use this site as a resource, there is more information available on all of the topics above. Lua 5.1 Reference Manual - contents
Orwell #3
Posted 06 November 2012 - 10:26 AM
1. Tables are containers of other variables. These variables are called values. The values can be stored at certain keys. So the key identifies the value. In simple tables, the key is just the index (1,2,3,…).
They are indeed defined like this:

tbl = {'value1','otherValue','andAnotherOne'}
or with the keys explicitly:

tbl = {1='value1',2='otherValue',3='andAnotherOne'}

Then you can access them like this:

secondValue = tbl[2]
lastValue = tbl[3]
i=1
firstValue = tbl[i]

Now with custom keys it could be like this:

tbl = {'color'=colors.red(), 'name'='Me','age'=42}
name = tbl['name']
years = tbl.age

Notice the two ways of accessing a value at given key ([*]or .*).

At last, you can also change the keys and values after the declaration of the table:

tbl = {'name'='Me'}
tbl.hobby = 'programming'
tbl.oldName = tbl.name
tbl['name'] = 'Still Me'

So again, the [] notation is the same as the dot notation. Also, both keys and values can be anything, also other tables or functions. :P/>/> (e.g., 'term' is in fact a table, and term['write'] or term.write is an entry in that table with key 'write' and a function as value)

2. tonumber(arg) and tostring(arg) are functions that take arg as an argument and change them to a number/string respectively.
For example:

str = "1"
num = tonumber(str)
sum = num + 1   -- works, 'num' is a number and 1 too, so you can add them
sum = str + 1	  -- doesn't work, you're trying to add a string and a number :D/>/>

3. It is 'in pairs()'. (I just see that luanoob posted the answer, so I'll pass on this one :)/>/> )
cheekycharlie101 #4
Posted 06 November 2012 - 10:41 AM
thanks soooo much guys ! :P/>/>