|
| 30 Jul 2017 12:06 PM |
Why can I do
for i,v in pairs{...} do
but not
for i,v in next{...} do
... is for variadic functions, I'm calling it in a variadic function |
|
|
| Report Abuse |
|
|
| |
|
|
| 30 Jul 2017 02:37 PM |
| Can anyone answer this? I thought pairs just used the next function, so wouldn't it be the same? I'm a lil confused |
|
|
| Report Abuse |
|
|
Pinkerten
|
  |
| Joined: 03 Aug 2014 |
| Total Posts: 840 |
|
| |
|
|
| 30 Jul 2017 02:48 PM |
Can you explain why it errors, though?
14:48:02.765 - Workspace.Script:2: attempt to call a number value |
|
|
| Report Abuse |
|
|
Pinkerten
|
  |
| Joined: 03 Aug 2014 |
| Total Posts: 840 |
|
|
| 30 Jul 2017 02:51 PM |
Honestly, I can't. I've used it before and met people who swear to it, but I know many more people far more intelligent than me who use in pairs. |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 30 Jul 2017 02:57 PM |
| Ignore Pink, he doesn't know what he's talking about It heavily depends on what you're trying to do. ipairs iterates over only integer keys, so if you have a mixed dictionary/array like this... table = {"thing1","thing2","thing3",["something else"] = true, ["another defined thing"] = "hello"} for i,v in ipairs(table) do print(i,v) end >1 thing1 >2 thing2 >3 thing3 It would totally ignore the definitions Pairs and Next are basically the same. Pairs calls Next, it's just there so that you can use the parenthesis around your table if that's what you're into. It will print both integer and noninteger keys. Keep in mind noninteger keys have no order. for i,v in next,table do print(i,v) end OR for i,v in pairs(table) do print(i,v) end will both print the same thing: >1 thing1 >2 thing2 >3 thing3 >something else true >another defined thing hello Answering scripting questions ## ####### ########################### |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 30 Jul 2017 02:58 PM |
Ignore Pink, he doesn't know what he's talking about
It heavily depends on what you're trying to do.
ipairs iterates over only integer keys, so if you have a mixed dictionary/array like this...
table = {"thing1","thing2","thing3",["something else"] = true, ["another defined thing"] = "hello"}
for i,v in ipairs(table) do print(i,v) end >1 thing1 >2 thing2 >3 thing3
It would totally ignore the definitions
Pairs and Next are basically the same. Pairs calls Next, it's just there so that you can use the parenthesis around your table if that's what you're into. It will print both integer and noninteger keys. Keep in mind noninteger keys have no order.
for i,v in next,table do print(i,v) end
OR
for i,v in pairs(table) do print(i,v) end
will both print the same thing:
>1 thing1 >2 thing2 >3 thing3 >something else true >another defined thing hello
|
|
|
| Report Abuse |
|
|
Pinkerten
|
  |
| Joined: 03 Aug 2014 |
| Total Posts: 840 |
|
|
| 30 Jul 2017 03:02 PM |
| ^I forgot to use a comma. for i in next,{"Corn"} do print(i) end # ## ##### still stands. Don't use next. |
|
|
| Report Abuse |
|
|
|
| 30 Jul 2017 03:03 PM |
Ah, I understand now
so doing
for i,v in next,{...} do
works fine
Thanks for clearing that up |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 30 Jul 2017 03:04 PM |
There is NO problem with next Pairs calls next. Don't mislead him. |
|
|
| Report Abuse |
|
|
Pinkerten
|
  |
| Joined: 03 Aug 2014 |
| Total Posts: 840 |
|
|
| 30 Jul 2017 03:06 PM |
^ I never said there was a problem did I? And there is. It looks bad. Thats it. There is no other reason to use pairs over next other than next looks ugly. So I use pairs, and I agree with others that it is much more readible. |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 30 Jul 2017 03:08 PM |
It doesn't look bad, are you dense? If you have a problem with reading the difference, you got a bone in the middle of your head.
How can you stand adding those args to For? for i,v in -- you should have an issue with this if you have an issue with in next,thing do |
|
|
| Report Abuse |
|
|
|
| 30 Jul 2017 03:08 PM |
| Yeah, that's why I asked because I knew that pairs called next, I was just confused on why it wasn't working the same way, but it totally makes sense now. |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 30 Jul 2017 03:10 PM |
Yeah, you can use both, it's a matter of preference.
To say "don't use next over pairs", that's horribly unhelpful and makes it seem like next is inferior to pairs.
It is totally a choice. |
|
|
| Report Abuse |
|
|