### Author Topic: Newbee & Tables  (Read 1962 times)

0 Members and 1 Guest are viewing this topic.

#### gaiza

• Newbie
• Posts: 3
• Karma: +0/-0
##### Newbee & Tables
« on: 27 May, 2005, 22:56:11 »
Hello, this is my first post, so hello everybody.

My question is:

I have a fuction that return a table

TheFuncProvided() -- (args - 0, results - 1 (table of key(string).value(boolean))

so to get the result i wrote this :

Table = TheFuncProvided()

if Table then

if Val then
--do somthing
end
end

As resukt Val is always nil, but table is not empty.

What i'm doing wrog ?

#### PtokaX forum

##### Newbee & Tables
« on: 27 May, 2005, 22:56:11 »

#### gaiza

• Newbie
• Posts: 3
• Karma: +0/-0
##### (No subject)
« Reply #1 on: 29 May, 2005, 21:42:10 »
Well, nobody know anything about arrays ?

#### Dessamator

• Scripter
• Emperor
• Posts: 1 262
• Karma: +13/-9
##### (No subject)
« Reply #2 on: 29 May, 2005, 21:52:49 »
hmm, its probably because ppl are confused by ur "problem", in lua arrays are more or less like tables,

to assign a value to a table in lua u do it more or less like this :

Code: [Select]
`table["stuff"] = 1`

to read a key from a table u do it more or less like this:

Code: [Select]
`val =table.stuff , orval =table["stuff"]`

or u can use a loop to acess the info in a table something like e.g:

Code: [Select]
`local temp,temp2 =nil,nilfor index,value in table dotemp =temp.."index "temp2 =temp2.."value "end`
P.S. u cant just assign a function to a table, unless a function returns a value which u can add to a table !
hope it helps with ur dilema !
Ignorance is Bliss.

#### plop

• Forum God
• Posts: 2 464
• Karma: +37/-0
##### (No subject)
« Reply #3 on: 30 May, 2005, 13:29:05 »
here's a example script 2 give you an idea of what you want 2 do.
!this and !that work as main chat commands and are called from a table.
the way you were trying allready failed on TheFunctionProv, as that isn't how you declare a function in lua.
and using an array for these this isn't handy, you would always have 2 loop the array, while tables can be hashed.
and like dessamator said, array's are tables in lua which are indexed by numbers.
for example:
Array = {var1, var2, var3, var4}
is inside lua
Array = {1=var1, 2=var2, 3=var3, 4=var4}

here's the example script for functions inside tables.
Code: [Select]
`tTable = { ["this"] = function(var1, var2) SendToAll("botje", "doing this") return 1 end, ["that"] = function(var1, var2) SendToAll("botje", "doing that") return 1 end,}function ChatArrival(user, data) local s,e,sCmd = string.find(data, "%b<>%s+%p(%S+).*|") if sCmd and tTable[sCmd] then if tTable[sCmd](user, data) then return 1 end endend`
if you want more examples then check my freshstuff and texter series or zcrazy.
these all use functions which are stored in tables.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

#### gaiza

• Newbie
• Posts: 3
• Karma: +0/-0
##### (No subject)
« Reply #4 on: 30 May, 2005, 21:05:52 »
Thanks gys, i'll check if ican get it work.

Thanks again, i'll keep in touch

#### PtokaX forum

##### (No subject)
« Reply #4 on: 30 May, 2005, 21:05:52 »