|
| 07 Jan 2016 05:46 PM |
I've asked this question before on Scripting Helpers, but I didn't get an answer to my question. :c
I posted this question because I was curious on something.
This should sum a lot of it up: "I've seen 'for v in next, Table do', and 'for i, v in pairs (Table) do', so what happens if I set both of them up oppositely?"
After setting up the code like this, it crashed when I ran it until I put a 'wait' the next time, but when I did, it returns this in the Output: 'function: 0EDC2EB8 table: 0EFAD730'
This is the code I had used:
local Table = { "Player1", "Player2", "Player3" }
for i, v in pairs, Table do print(i, v) wait(5) end
However, when I tried this code:
local Table = { "Player1", "Player2", "Player3" }
for i, v in next (Table) do print(i, v) end
It only returned an error instead of crashing.
Why does code #1 return such Output, while code #2 only errors & breaks the code? Why does switching the two types cause such things to occur?
Thanks for reading. :) |
|
|
| Report Abuse |
|
|
vlekje513
|
  |
| Joined: 28 Dec 2010 |
| Total Posts: 9057 |
|
|
| 07 Jan 2016 05:48 PM |
| Err, Scripting Helpers is gone for a long, long time already. |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2016 05:52 PM |
| You can still access Scripting Helpers, but I just prefer not to really ask questions there anymore, as some of my questions have gone months without answers. :( |
|
|
| Report Abuse |
|
|
lysandr
|
  |
| Joined: 01 Aug 2013 |
| Total Posts: 236 |
|
|
| 07 Jan 2016 05:53 PM |
Because scripting is b0gus and likes to cokblock certain people. Just kidding. I'm pretty sure you're printing an index which returns a severe line of raw coding. "for i, v in next (Table) do print(i, v) end" That means you're printing index and you'll just get the raw code or whatever you wanna call it. Try this. What it used to look like for i, v in next (Table) do print(i, v) end What it should look like for i, v in next (Table) do print(v) end I hope this helps. Sincerely, yours truly. |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2016 06:02 PM |
Using the new code, it sent back an error in the Output with the following: '5: attempt to call a number value.' Why does it think I tried to return a number value? o_e
This is how I setup the code if that helps:
local Table = { "Player1" }
for i, v in next (Table) do print(v) wait(5) end |
|
|
| Report Abuse |
|
|
lysandr
|
  |
| Joined: 01 Aug 2013 |
| Total Posts: 236 |
|
|
| 07 Jan 2016 06:47 PM |
Use this instead.
What it used to look like. local Table = { "Player1" }
for i, v in next (Table) do print(v) wait(5) end What it should look like. local Table = { "Player1" }
for i, v in pairs (Table) do print(v) wait(5) end I hope this helps.
Sincerely, yours truly |
|
|
| Report Abuse |
|
|
Darkenus
|
  |
| Joined: 17 Jul 2014 |
| Total Posts: 1997 |
|
|
| 07 Jan 2016 06:48 PM |
for i, v in next (Table) do print(i, v) end
>> It's 'for i,v in next, Table do'
You put a comma after next, and do not use parenthesis.
correct waY;
for i, v in next, Table do print(i, v) end
|
|
|
| Report Abuse |
|
|
|
| 07 Jan 2016 09:38 PM |
I'm sorry, but the last two replies did not answer my question. :(
The question was 'Why does code #1 return such Output, while code #2 only errors & breaks the code? Why does switching the two types cause such things to occur?', thus my question 'Why do these for loops react differently when set like this?'
Thanks for reading. :) |
|
|
| Report Abuse |
|
|
chimmmihc
|
  |
| Joined: 24 Jul 2014 |
| Total Posts: 2420 |
|
|
| 07 Jan 2016 10:52 PM |
| You're all dumb, put a comma after next and it works fine |
|
|
| Report Abuse |
|
|
Darkenus
|
  |
| Joined: 17 Jul 2014 |
| Total Posts: 1997 |
|
|
| 07 Jan 2016 11:26 PM |
| @Chim, I said that..... -.- |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 07 Jan 2016 11:27 PM |
chimmmmihc you're calling them dumb yet you don't even understand why lmfao. But yes: for i, v in next, Table do
Although why you are switching from pairs in the first place is a mystery. |
|
|
| Report Abuse |
|
|
Link5659
|
  |
| Joined: 04 Jun 2012 |
| Total Posts: 4525 |
|
|
| 07 Jan 2016 11:32 PM |
| How do we get into scripting helpers? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 07 Jan 2016 11:35 PM |
scriptinghelpers dot org
Unless you're talking about the subforum that no longer exists? |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2016 11:38 PM |
| I think I killed you once.. |
|
|
| Report Abuse |
|
|
lysandr
|
  |
| Joined: 01 Aug 2013 |
| Total Posts: 236 |
|
|
| 08 Jan 2016 10:26 AM |
What it should look like to work. local Table = { "Player1", "Player2", "Player3" }
for i, v in pairs (Table) do print(v) end Doing this "print(i,v)" would print an index. i is named i because it stands for 'index'. v is variable or value. I don't know why anyone would want to print the index, but that's because I'm relatively new to scripting. local Table = { "Player1", "Player2", "Player3" }
for i, v in next (Table) do print(i, v) end This is crashing because you're replacing "next" with "pairs". The line is "for i,v in pairs do". And for all of the 'know-it-alls' out there; there doesn't need to be a comma after the "in pairs" at all. How do I know this? Because I've tested this script 10 times already, in and out of studio and it gives me the table each time. I hope this helps.
Sincerely, yours truly |
|
|
| Report Abuse |
|
|
|
| 08 Jan 2016 05:46 PM |
I already know the for loops are set up as the following:
local Table = { "Player1" }
for i, v in pairs(Table) do print(i, v) end
for i, v in next, Table do print(i, v) end
I've been replied about this a lot, when I stated the following in the beginning of my explanation for why I set them up like that: "I've seen 'for v in next, Table do', and 'for i, v in pairs (Table) do', so what happens if I set both of them up oppositely?" :(
But, my question is why the two codes, when they were set up differently, crashed and/ or returned an error when I fired the code, and why it returned in the Output 'function: 0E2C54E8 table: 13C47560' when I applied a 'wait' to one of the codes?
Thanks for reading. :) |
|
|
| Report Abuse |
|
|
|
| 08 Jan 2016 05:52 PM |
@lysandr
But why does it return 'function: 0E2C54E8 table: 13C47560' (this is when I fire the code today) when I apply i and v to the print? (function: Code and table: Code are i and v), and why for the other for loop, does it error saying I returned a number value? (5: attempt to call a number value)
For loop returning Function & Table:
for i, v in pairs, Table do print(i, v) wait(5) end
For loop returning error:
for i, v in next (Table) do print(i, v) end
Thanks for reading. :) |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jan 2016 05:55 PM |
"And for all of the 'know-it-alls' out there; there doesn't need to be a comma after the "in pairs" at all. How do I know this? Because I've tested this script 10 times already, in and out of studio and it gives me the table each time." No one said you did dude, they said after next. Get your eyes checked.
"But, my question is why the two codes, when they were set up differently, crashed and/ or returned an error when I fired the code, and why it returned in the Output 'function: 0E2C54E8 table: 13C47560' when I applied a 'wait' to one of the codes?" Because of the way generical for loops work.
The syntax for these types of loop is like this: for var_list in iterator, params do end
Where next is your iterator which returns the next key and value from the previous one. The reason this works: for i,v in pairs(tbl) do
Is because pairs is really actually just a wrapper for "next", it's almost exactly equivalent to:
function pairs(tbl) return next, tbl end
So it's next doing all the hard work. So when you do: "for i,v in pairs(tbl) do" The pairs(tbl) is actually evaluated first, which returns the next iterator and the table so after the first function call, it's actually the same as "for i,v in next, tbl do" |
|
|
| Report Abuse |
|
|
|
| 08 Jan 2016 06:22 PM |
@cntkillme
Thanks man! That actually explains a lot! :D Although, why does the other one return an error saying I gave it a number value, when I gave it a table? That one I'm confused about still. o_e |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jan 2016 06:53 PM |
Because next is a function (like all iterators).
When you call next, it returns the next key and value (in this case key is a number).
So when you do this: local Table = {"a"}; for i, v in next (Table) do print(i, v) end
It's actually going to pretty much result in this: for i, v in 1, "a" do which of course, makes no sense.
Generic for loops rely on functions to work, which is why when you wrap next in parenthesis (aka calling it), this call happens before the loop actually starts (like in the case of for i,v in pairs(table) do). |
|
|
| Report Abuse |
|
|
|
| 08 Jan 2016 08:09 PM |
@cntkillme
That makes sense. :o Thanks a lot for the deep explanation! It really helped, and answered my question! :D |
|
|
| Report Abuse |
|
|