English "Table" example

22 replies
Goto Page
To the start Previous 1 2 Next To the start
26.12.12 08:49:46 pm
Up
EndDead
User
Offline Off
Hello all.
As some of you know, I'm learning how to script. And i'm still a newbie, I'm trying to figure out how to use a table like this:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
EXAMPLE = {
     {43, 23, 24} -- TileX, TileY, Item ID
     {13, 43, 45} -- TileX, TileY, Item ID
[...]
}
     --Question here--

addhook("use","enddead_use") -- "Use" is for buttons
function enddead_use(id)
--(Get table)
(for [1] in table set X)
(for [2] in table set Y)
(for [3] in table get Item ID)
(if [1] and [2] is true then)
equip(id, [3])
-- Do the same for other tables (And for new tables)

While ([1]) = the first number in a table, and ([2]) same but 2nd number, and same for ([3]) but for the 3rd number in the table.

Thanks in advance~
26.12.12 08:58:57 pm
Up
Infinite Rain
Reviewer
Offline Off
Well you have to loop through the table, to know if the coordinates right or not.

Use for loop.

Example of loop
Code:
1
2
3
for id = 1, 32 do
     msg(id)
end

Will give you
Quote:
1
2
...
30
31
32


id will be updated by 1 each time till it hits 32

so using loop you can scan table.

Code:
1
2
3
4
5
6
-- #<table> will return you the ammount of elements in your table
for tid = 1, #EXAMPLE do
     if EXAMPLE[tid][1] == player(id, 'tilex') and EXAMPLE[tid][2] == player(id, 'tiley') then
          parse('equip '.. id ..' '.. EXAMPLE[tid][3])
     end
end



Hope you understand me, and if not...
Then
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
26.12.12 09:09:23 pm
Up
EndDead
User
Offline Off
@user Infinite Rain: Yeah, I understood (At least most of what you said )
I'm trying that tomorrow, its pretty late here Thanks anyway.
When I try, I will give my results (And any other questions if I had)
26.12.12 10:55:03 pm
Up
Yates
Reviewer
Offline Off
@user Infinite Rain: I guess while is better to use as with for you will need to enter an ending number plus you can give it its own adding value. While loop stops if it is nil; and you can always break it like for.

Then again a working code is a good code. So use what Factis posted and you can learn the other things later.
26.12.12 11:09:29 pm
Up
EP
User
Offline Off
@user Yates: while is an indefinited iterative function, which is not recommended to use in case of looping a table.

If you're going to use while to loop a table then your code will look like this:

Code:
1
2
3
4
5
i = 1
while i < #table do
     --Block
     i = i + 1
end


Therefore it's better to use for to loop a table because it simplifies the code.

@user Flacko: Haha
edited 2×, last 26.12.12 11:15:57 pm
26.12.12 11:10:31 pm
Up
Flacko
User
Offline Off
user Yates has written:
@user Infinite Rain: I guess while is better to use as with for you will need to enter an ending number plus you can give it its own adding value. While loop stops if it is nil; and you can always break it like for.

Then again a working code is a good code. So use what Factis posted and you can learn the other things later.


No it's not
Code:
1
for var=val, max[, increment]

is the syntax for the for loop, being the increment variable optional. In most cases you won't need a while loop (unless you don't know what will be your max)

@user EP: Damn, you ninja'd me twice.
edited 1×, last 26.12.12 11:13:06 pm
26.12.12 11:13:04 pm
Up
Yates
Reviewer
Offline Off
But if he is going to add more tile positions/item ids then wouldn't it be better to use a while loop instead of having to keep changing the max of the for loop? As while will end itself if the return value is nil.
26.12.12 11:18:02 pm
Up
EP
User
Offline Off
@user Yates: What if a value of the table is supposed to be nil? The loop will break there, if you use for instead it will loop through the table depending on it's lengh, not the value.
26.12.12 11:18:54 pm
Up
Yates
Reviewer
Offline Off
But he is going to use tile positions and item ids, that is why I said in my first post;

user Yates has written:
I guess while is better to use


In this case, that is.
edited 1×, last 26.12.12 11:19:31 pm
26.12.12 11:19:17 pm
Up
Flacko
User
Offline Off
If he's going to add more indices to his table, he should better use
Code:
1
2
3
for i=1, #t do
     stuff
end

That way all that lua has to do is get the table length once (table lengths are stored in a special index so it's just like accessing a key) and increment i every loop

On the other hand
Code:
1
2
3
4
5
var i=1
while t[i] do
     stuff
     i = i+1
end

Lua has to index the table every loop, plus it will have to create the var i at a higher scope and increment it, once again, every loop

While loops tend to be slower than for loops in lua.
26.12.12 11:20:29 pm
Up
EngiN33R
Moderator
Offline Off
To add to user Flacko's post - while loops are indeed slower. I did some benchmarking, and the results are in the More box.

Damn, six posts while I was doing this. You are too quick, guys.

More >
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
26.12.12 11:23:01 pm
Up
Yates
Reviewer
Offline Off
Oh, all I knew is how to use them and how they were both different, I did not think about the speed nor did I really know how they execute (In such detail as Flacko posted).

Thanks I guess.
edited 1×, last 26.12.12 11:24:56 pm
26.12.12 11:24:28 pm
Up
EP
User
Offline Off
Best way @user EndDead: might use to loop his table is pairs method.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
EXAMPLE = {
     {43, 23, 24}, -- TileX, TileY, Item ID
     {13, 43, 45} -- TileX, TileY, Item ID
}
addhook("use","_use")
function use(id,x,y)
     for k,v in pairs(EXAMPLE) do
          if x == v[1] and y == v[2] then
               parse("equip "..id.." "..v[3])
          end
     end
end


Anyways there are much ways to loop tables
edited 1×, last 27.12.12 03:51:38 am
27.12.12 01:38:07 am
Up
sheeL
User
Offline Off
@user EP: Change

Code:
1
2
3
4
EXAMPLE = {
     {43, 23, 24} -- TileX, TileY, Item ID
     {13, 43, 45} -- TileX, TileY, Item ID
}


to

Code:
1
2
3
4
EXAMPLE = {
     {43, 23, 24}, -- TileX, TileY, Item ID
     {13, 43, 45} -- TileX, TileY, Item ID
}


You forgot the ,
27.12.12 03:28:20 am
Up
EP
User
Offline Off
@user sheeL: I've copypasted what user EndDead: wrote as an example and I didn't pay attention to that, thanks.
27.12.12 04:11:57 am
Up
omg
User
Offline Off
oh yeah, good catch...accessing the local counter will be slower than accessing the for loop counter because its in a higher scope. didnt even occur to me

in a situation like this, pairs would be the best choice since accessing keys is faster than indices
will code for food
27.12.12 08:26:26 am
Up
EndDead
User
Offline Off
Er, Much information.
Thanks all. Gonna open this thread from time to time to learn what I have missed. And I'm trying it soon and giving my results (And any other questions related).

Thanks again~
27.12.12 12:50:44 pm
Up
Infinite Rain
Reviewer
Offline Off
@Yates
You always are able to get table's ammount of elements, therefore for loop is better than while loop to use in this case.
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
28.12.12 06:04:27 pm
Up
EndDead
User
Offline Off
I still seem not to be able to make a working one
Could anyone post a working example?

(BTW, I'm not planing to use this method on any script. Just as an example to learn from it)

@user EP: LUA ERROR: Attempt to call a nil value .. ? hmm
28.12.12 06:48:29 pm
Up
Shawni
User
Offline Off
@user EndDead:
Because of v[1] , v[2] and v[3]
We die once, do i have to hide to be visible ?
To the start Previous 1 2 Next To the start