|
| 05 Mar 2015 06:04 PM |
| What is the exact point of using In Pairs in a script and what can you use it for? |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:04 PM |
| Is is an iterator function. You use it in generic for loops. |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:08 PM |
| So I looked at it on the wiki... So it stops when there are no more values to print in the item :/ Is it possible you could give an example the one in the wiki makes sense yet I still cant get the idea. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 05 Mar 2015 06:08 PM |
for i,s in pairs (game.StarterPack:GetChildren()) do if s:IsA("Tool") then s:Remove() end end
--iterates every single thing inside the given variable (Starterpack) and puts them in "pairs" or rather for every item, add the variable s.
--finds every tool in the starterpack and removes it. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 05 Mar 2015 06:10 PM |
| instead of using i (index), use something like _,s in pairs instead so you can use the variable i for something else in the code. |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:11 PM |
| So all its children = the variable s. Right? |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
|
| 05 Mar 2015 06:16 PM |
| Is there a way you could use this and give each one a different variable name so you could find one individually or give it a numerical value and print the number of children |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:23 PM |
for i,v in pairs(table)do print(v) -- v as in value print(i) -- i as in index, the place on the table the value is it end |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:24 PM |
woah... we got the same hat... anywho,
local ting = 0 for _,v in pairs(game.workspace:GetChildren())do ting = ting + 1 print (v,ting) end
output:
1 2 3 4 5 6 7 8 9
and so on |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:24 PM |
| Ok thanks I've been wondering the point of pairs for awhile now I think all my questions are answered for now. :O |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 05 Mar 2015 06:25 PM |
I know the way I do my for loops, its probably inefficient but it still gets the job done nonetheless. The loop counts everything for the given variable as v. If you want to define something else with a different variable then you will have to use another loop. Variable v could be anything, A part, a model, whatever it is in the game.
lets say there are 5 bicks in the game.
Bricks are called 1,2
for i,v in pairs(game.Workspace:GetChildren()) do if v.Name == "1" then print("we found brick 1") elseif v.Name == "2" then print("we found brick 2" else --if both statements are false print("we found none of the bricks in the game") end end
|
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:25 PM |
@Andrew
Not the best way to explain it.
Anyway what it does is go thiough evenything in the table, and the value in the table is the second variable and the place in it is the first, these can be named anything you want. |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:27 PM |
| FunkiAndrew, I agree with MrJoey. You shouldn't teach it that way. You get the index variable for a reason, and then you go and create a system that would be much simpler (and better (and even better when teaching)) to use the index variable. |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:30 PM |
yeah...
for loops were tricky for me, they got me confused.
for _,v in pairs(workspace:GetChildren())do -- btw, 'workspace' can be used instead of game.Workspace print (v.Name) end
prints the same thing as
for _,v in next, workspace:GetChildren() do print (v.Name) end
some people prefer next, others prefer pairs. it's really your choice. of course, there IS ipairs, which I don't have much experience with... |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:31 PM |
so for example it'd be like this? t = (1,2,3,4,5) for _, v in pairs do (t) print (t) ----------------------------- the output would be 1 2 3 4 5 right? |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 05 Mar 2015 06:32 PM |
parts = game.Workspace:GetChildren()
for i=1,#parts do print(i) end
|
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:33 PM |
for i,v in pairs(TABLE_NAME) do print(v.Name..' Is at the position of '..i..' In the table') end |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:35 PM |
I understand for loops using numerical values to know how many times it will do it. like for i = 1, 2 do print "easy" it will print it two times I just never understood pairs but I think I understand it now. |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:37 PM |
so a pairs loop would go like t = (1,2,3,5,6) for _, v in pairs (t) do print (t) -------------- Output 1 2 3 5 6 Right? |
|
|
| Report Abuse |
|
|
|
| 05 Mar 2015 06:38 PM |
_,v
i,v
potato,corn
-- values print(v)
print(v)
print(corn)
-- numbers print(potato)
print(i)
|
|
|
| Report Abuse |
|
|
| |
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 05 Mar 2015 06:40 PM |
A table is just a list of key-value pairs. You give a table a key (like tbl[key]) and it gives you a value, or you set the value for a key (like tbl[key] = value).
A dictionary is something like local person = { name = "dr01d3k4"; id = 72297; favouriteColour = "blue"; };
An array looks like: local array = {"hello", "world", "this", "is", "an", "array"}; but this is just shorthand for local array = {[1] = "hello", [2] = "world", [3] = "this", [4] = "is", [5] = "an", [6] = "array"};
The simple explanation of what pairs does is allow you to iterate through these key-value pairs: for key, value in pairs(person) do print(key, value); end This would output (tested in online Lua 5.3 demo) id 72297 name dr01d3k4 favouriteColour blue
Note that dictionaries don't guarantee the order will be the same as how you wrote it.
|
|
|
| Report Abuse |
|
|