Jump to content




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

help utility lua

19 replies to this topic

#1 ChaddJackson12

  • Members
  • 264 posts

Posted 18 October 2012 - 04: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.

#2 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 18 October 2012 - 04:42 PM

There really is no difference, I prefer pairs though.
They have some differences but they're both loops..

#3 ChaddJackson12

  • Members
  • 264 posts

Posted 18 October 2012 - 04:42 PM

View PostNoodle, on 18 October 2012 - 04:42 PM, said:

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.

#4 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 18 October 2012 - 04:44 PM

View PostChaddJackson12, on 18 October 2012 - 04:42 PM, said:

View PostNoodle, on 18 October 2012 - 04:42 PM, said:

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

#5 billysback

  • Members
  • 569 posts

Posted 18 October 2012 - 04: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...ad.php?t=875909

#6 ChaddJackson12

  • Members
  • 264 posts

Posted 18 October 2012 - 04:49 PM

View Postbillysback, on 18 October 2012 - 04:46 PM, said:

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

#7 ChaddJackson12

  • Members
  • 264 posts

Posted 18 October 2012 - 04:51 PM

View PostNoodle, on 18 October 2012 - 04:44 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:42 PM, said:

View PostNoodle, on 18 October 2012 - 04:42 PM, said:

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.

#8 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 18 October 2012 - 04:53 PM

View PostChaddJackson12, on 18 October 2012 - 04:51 PM, said:

View PostNoodle, on 18 October 2012 - 04:44 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:42 PM, said:

View PostNoodle, on 18 October 2012 - 04:42 PM, said:

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


#9 ChaddJackson12

  • Members
  • 264 posts

Posted 18 October 2012 - 04:54 PM

View PostNoodle, on 18 October 2012 - 04:53 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:51 PM, said:

View PostNoodle, on 18 October 2012 - 04:44 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:42 PM, said:

View PostNoodle, on 18 October 2012 - 04:42 PM, said:

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?

#10 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 18 October 2012 - 04:55 PM

View PostChaddJackson12, on 18 October 2012 - 04:54 PM, said:

View PostNoodle, on 18 October 2012 - 04:53 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:51 PM, said:

View PostNoodle, on 18 October 2012 - 04:44 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:42 PM, said:

View PostNoodle, on 18 October 2012 - 04:42 PM, said:

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

#11 ChaddJackson12

  • Members
  • 264 posts

Posted 18 October 2012 - 04:57 PM

View PostNoodle, on 18 October 2012 - 04:55 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:54 PM, said:

View PostNoodle, on 18 October 2012 - 04:53 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:51 PM, said:

View PostNoodle, on 18 October 2012 - 04:44 PM, said:

View PostChaddJackson12, on 18 October 2012 - 04:42 PM, said:

View PostNoodle, on 18 October 2012 - 04:42 PM, said:

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/>

#12 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 18 October 2012 - 04:58 PM

View PostChaddJackson12, on 18 October 2012 - 04:57 PM, said:

Thanks, this will help me out a ton. :P/>

Your welcome, always here to help!

#13 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,401 posts

Posted 18 October 2012 - 05: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.

#14 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 October 2012 - 05: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().

#15 billysback

  • Members
  • 569 posts

Posted 18 October 2012 - 05: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)

#16 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 October 2012 - 05: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.

#17 Ditto8353

  • New Members
  • 138 posts

Posted 18 October 2012 - 05:32 PM

Read up on iterators.
Then you can just make your own to do all kinds of weird stuff.

#18 ChunLing

  • Members
  • 2,027 posts

Posted 18 October 2012 - 05: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.

#19 billysback

  • Members
  • 569 posts

Posted 18 October 2012 - 05: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/>

#20 Ditto8353

  • New Members
  • 138 posts

Posted 18 October 2012 - 06:03 PM

You can do amazing things with tables.
Spoiler

Spoiler

Spoiler

Edit: Tables can replace the Switch-Case from C-Based languages!
Spoiler






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users