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

For k, v in pairs() / ipairs()

Started by ChaddJackson12, 18 October 2012 - 03:41 PM
ChaddJackson12 #1
Posted 18 October 2012 - 05:41 PM
This is no big deal at the moment. But I would like to know what these statements mean:

for k, v in pairs()
-- And
for k, v in ipairs()
I have looked around and do not really get what they do. So I came here.

If you can please explain what each one does, or how it would be used. Please help when possible. Thanks in advance.
Noodle #2
Posted 18 October 2012 - 05:42 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
ChaddJackson12 #3
Posted 18 October 2012 - 05:42 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
Loops to do what? I saw it be used once, and it printed out a list, sorta.
Noodle #4
Posted 18 October 2012 - 05:44 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
Loops to do what? I saw it be used once, and it printed out a list, sorta.
They loop through a table
tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do
print(k, v) – K = apples, oranges V = 2, 3
end
billysback #5
Posted 18 October 2012 - 05:46 PM
this post explains it brilliantly (You need to know what ipairs and pairs actually do in order to understand it though…)
http://facepunch.com/showthread.php?t=875909
ChaddJackson12 #6
Posted 18 October 2012 - 05:49 PM
this post explains it brilliantly (You need to know what ipairs and pairs actually do in order to understand it though…)
http://facepunch.com...ad.php?t=875909
That is what I am looking for. What "pairs" and "ipairs" do exactly
ChaddJackson12 #7
Posted 18 October 2012 - 05:51 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
Loops to do what? I saw it be used once, and it printed out a list, sorta.
They loop through a table
tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do
print(k, v) – K = apples, oranges V = 2, 3
end
Ok I actually ran that program. I kind of get the idea now. But, how could this be used with fs.list()? To find out if only 1 file exists in the path.
Noodle #8
Posted 18 October 2012 - 05:53 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
Loops to do what? I saw it be used once, and it printed out a list, sorta.
They loop through a table
tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do
print(k, v) – K = apples, oranges V = 2, 3
end
Ok I actually ran that program. I kind of get the idea now. But, how could this be used with fs.list()? To find out if only 1 file exists in the path.
tbl = fs.list("") -- Main directory
for k, v in pairs(tbl) do
  print(v) -- Prints all the files in the directory
end
ChaddJackson12 #9
Posted 18 October 2012 - 05:54 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
Loops to do what? I saw it be used once, and it printed out a list, sorta.
They loop through a table
tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do
print(k, v) – K = apples, oranges V = 2, 3
end
Ok I actually ran that program. I kind of get the idea now. But, how could this be used with fs.list()? To find out if only 1 file exists in the path.
tbl = fs.list("") -- Main directory
for k, v in pairs(tbl) do
  print(v) -- Prints all the files in the directory
end
Ok, stupid question: K in the table stands for what? Also, V in the table stands for what? K for the left? V For the right? Something like this?
Noodle #10
Posted 18 October 2012 - 05:55 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
Loops to do what? I saw it be used once, and it printed out a list, sorta.
They loop through a table
tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do
print(k, v) – K = apples, oranges V = 2, 3
end
Ok I actually ran that program. I kind of get the idea now. But, how could this be used with fs.list()? To find out if only 1 file exists in the path.
tbl = fs.list("") -- Main directory
for k, v in pairs(tbl) do
  print(v) -- Prints all the files in the directory
end
Ok, stupid question: K in the table stands for what? Also, V in the table stands for what? K for the left? V For the right? Something like this?
K = Key - Apples
V = Variable - 2
ChaddJackson12 #11
Posted 18 October 2012 - 05:57 PM
There really is no difference, I prefer pairs though.
They have some differences but they're both loops..
Loops to do what? I saw it be used once, and it printed out a list, sorta.
They loop through a table
tbl = { apples = 2, oranges = 3 }
for k, v in pairs(tbl) do
print(k, v) – K = apples, oranges V = 2, 3
end
Ok I actually ran that program. I kind of get the idea now. But, how could this be used with fs.list()? To find out if only 1 file exists in the path.
tbl = fs.list("") -- Main directory
for k, v in pairs(tbl) do
  print(v) -- Prints all the files in the directory
end
Ok, stupid question: K in the table stands for what? Also, V in the table stands for what? K for the left? V For the right? Something like this?
K = Key - Apples
V = Variable - 2
Thanks, this will help me out a ton. :P/>/>
Noodle #12
Posted 18 October 2012 - 05:58 PM
Thanks, this will help me out a ton. :P/>/>

Your welcome, always here to help!
Lyqyd #13
Posted 18 October 2012 - 06:07 PM
pairs() returns an iterator that will return the key and value of every key in the table, in no particular order. Usually, k and v are used to hold the key and value it returns. This is used to perform actions on each item in a table in turn, like when printing the contents of each value in the table.

ipairs() is very similar to pairs(), except that it will start at table[1] and iterate through all numerically indexed entries until the first nil value. It does this in order, which can be useful when you're looking for the first item in a list that meets certain criterion.
Kingdaro #14
Posted 18 October 2012 - 06:11 PM
There's actually a pretty big and important difference between pairs and ipairs besides speed.

Let's take our table, which contains both numeric and string indexes.

t = {
  [1] = 'eggs';
  [2] = 'cereal';
  [3] = 'pancakes';
  [5] = 'waffles'; -- I skipped 4 for a reason.

  myfavorite = 'bacon';
  myleastfavorite = 'toast';
}

With pairs(), it will print all indexes, and all values.

for i,v in pairs(t) do
  print(i,v)
end

With ipairs(), it will print only numbered indexes, and it stops if the next number in order does not exist.

In the above example table, I skipped 4. Because 4 does not exist, it will not go to 5.

for i,v in ipairs(t) do
  print(i,v)
end

Depending on your situation, one can always be more useful than the other. Most of the time though, I use pairs().
billysback #15
Posted 18 October 2012 - 06:20 PM
ipairs() does things systematically, it loops through the table in order.
pairs() does it in no numerical order.

(I read this somewhere but can't remember where)
Kingdaro #16
Posted 18 October 2012 - 06:31 PM
I'd also like to add that pairs prints the items in the table in the order they are defined/added.

So if you just drop items in a table the normal way ({item1, item2, item3}) it'll still print them in order.
Ditto8353 #17
Posted 18 October 2012 - 06:32 PM
Read up on iterators.
Then you can just make your own to do all kinds of weird stuff.
ChunLing #18
Posted 18 October 2012 - 06:37 PM
The point mentioned by Lyqyd and illustrated by Kingdaro is significant as well, ipairs only works for entries that are in order. Most of your early tables will be in order, but as you become more confident about table use you will often be using the keys to carry information as much (or sometimes more) than you are using the values.
billysback #19
Posted 18 October 2012 - 06:43 PM
wait, so does Lua have actual key values for tables?
I always presumed they had to be numbers and have been having to create my own key/value set ups…
I probably won't stop doing that but it's nice to know :P/>/>
Ditto8353 #20
Posted 18 October 2012 - 07:03 PM
You can do amazing things with tables.
Spoiler
local mf = "Muffins"
local t = {"Cake","Pie", mf}
print(t[1])
print(t[2])
print(t[3])

--[[ Outputs:
   Cake
   Pie
   Muffins
]]

Spoiler
local t = {[0] = "Cake","Pie", "Muffins"}
print(t[0])
print(t[1])
print(t[2])

--[[ Outputs:
   Cake
   Pie
   Muffins
]]

Spoiler
local t = {["favorite"] = "Pie", ["alsoGood"] = "Cake", "Muffins"}
print(t["favorite"])
print(t["alsoGood"])
print(t[1])
print("And one more " .. t.favorite)

--[[ Outputs:
   Pie
   Cake
   Muffins
   And one more Pie
]]

Edit: Tables can replace the Switch-Case from C-Based languages!
Spoiler
local eventHandler = { }
eventHandler["redstone"] = function (side)
   print("Redstone event on side: " .. side)
end
evtType,p1 = os.pullEvent()
eventHandler[evtType](p1)

--[[ On redstone event, outputs:
   Redstone event on side <side>
]]