|
| 13 May 2014 02:57 PM |
Can someone exsplain for in scripting for me please? The wiki dosen't help. If you have a good clear exsplanation of for please comment it.
I see stuff like for i,v in pairs or for _,r what is that stuff?? |
|
|
| Report Abuse |
|
|
|
| 13 May 2014 03:23 PM |
in pairs is a loop that goes through a table. A table is basically one variable with multiple values.
local tab = {1, 6, 15, 2}
You can access each value of a table like this:
print(tab[1]) >>1 print(tab[2]) >>6 print(tab[4]) >>2
If you want to do something with every value of a table, you can use an in pairs loop.
for index, value in pairs(tab) do --name index and value whatever you want. print(index .. ", " .. value) end
This loop's index will increase by one each time, and the value is equal to tab[index]. If you were to run that script, this is what would get printed out.
1, 1 2, 6 3, 15 4, 2 |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 May 2014 03:28 PM |
'This loop's index will increase by one each time' Not always, it will just get the next key. For example
local tab = {}; tab[5] = "hello";
for index, value in pairs(tab) do print(index, value); end
5 hello |
|
|
| Report Abuse |
|
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 May 2014 03:30 PM |
| OP, be sure, if you have non-numerical keys, you use pairs (or next) and not numeric for or ipairs. As those iterate over numerical-keys. |
|
|
| Report Abuse |
|
|
|
| 13 May 2014 05:10 PM |
| Thanks guys, cleared it up a bit. |
|
|
| Report Abuse |
|
|