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

What does this mean?:"for i,v in ipairs(t) do print(i,v) end"

Started by Zambonie, 19 January 2013 - 11:44 AM
Zambonie #1
Posted 19 January 2013 - 12:44 PM
So, I recently learned what are functions, and how to use them.(Remember,Im a noobie :D/> !)Ive also learned a little of tables.But,Ive reached to this part of the page Ive got it of,and,well,basilcly had this code:
for i,v in ipairs(t) do print(i,v) end
And well,basicly I dont know what the peice of code means.Can some one explain it to me?And also what are the uses of it and stuff?
Thnks if you do! :D/>

Page I got it off:http://lua-users.org/wiki/TablesTutorial
theoriginalbit #2
Posted 19 January 2013 - 01:00 PM
Ok so… When we have a table they are stored in whats called an index and value, or key and value (tables as dictionaries). Now the index or key means there is an easy way to access a value, for example tTable[3] would access the 3rd element in the table (this is an index) and tTable["button"] would access the element with the key "button"… when we are wanting to cycle through a table there are ( in Lua ) as standard incremental loop, an ipairs iterator, and a pairs iterator. Now it was said by someone ( I think gopher ) that ipairs has been depricated in the new version of lua as they realised that doing this is more efficient

for i = 1, #tTable do
  print( i, tTable[i] )
end
now what the ipairs and the pairs do is they access a table element, ipairs accesses the table sequentially ( hence why the above is the same and better ) and pairs accesses the table in a non-sequential order… when ipairs is used it returns 2 values, the index of the element and the value of the element ( i, v in the code example )… when pairs is used it also returns 2 values, the key of the element and the value of the element… so what the code example you posted does is it iterates through the table and prints the elements index and the value to the screen. however i do recommend using the code i supplied over ipairs since it is more efficient.
MudkipTheEpic #3
Posted 19 January 2013 - 01:01 PM
ipairs is a function that goes over the elements of a table, and returns the value in "v", as your code states. This is what that code does…


for i,v in ipairs(t) do --Iterates over the table t, returning v as the value.
print(i,v) -- Prints the value.
end

Edit: Ninjad…….
Edited on 19 January 2013 - 12:02 PM
theoriginalbit #4
Posted 19 January 2013 - 01:02 PM
ipairs is a function that goes over the elements of a table, and returns the value in "v", as your code states.
It also returns i
Zambonie #5
Posted 19 January 2013 - 01:13 PM
Even though im young,heres what I understood…That code "steals" the string/#/etc…from the the table its "assighned" with.And then it stores whatever it contains until it printed?Im a noobie so I dont understand most Of these words…Sorry :P/> ….
Zambonie #6
Posted 19 January 2013 - 01:15 PM
Just a note for tommorow:Dont exuse my writing because ill be writting on a tablet using 4G…..So….
theoriginalbit #7
Posted 19 January 2013 - 01:21 PM
pairs, ipairs go through a table and give the index and value in the current table element to i and v ( which can be called whatever you want, maybe k,v or this,that ), it does not remove or "steal" it, it stays in the table.
you dont have to print the data, you can do anything you want with the data, the reason they have it printing is its a nice easy example… you could have it do some maths based off it, or print given certain conditions, or whatever you want :)/>
Cranium #8
Posted 19 January 2013 - 01:33 PM

for i,v in ipairs(t) do
  --code
end
This means that for every (i)ndex and (v)alue in (numerically indexed)pairs(table) do some code.

The value ( i ) is in reference to the numerical value of the index(item number in the table).
The value ( v ) is in reference to the value stored in the index(this could be another table, a variable, a function, etc…)
Of course, these reference names are arbitrary, but I like to use i,v because they are easy to remember.
The difference with pairs and ipars is that ipars will work with numerically assigned indices only, and the pairs will work with all indices. A numerically assigned index is automatically assigned if no index value is assigned. so for the below table, t[1] would be "apple".
t = {"apple", "orange", "banana"}
Alternatively, you can define a table index with a variable name.
t = {apple = "apple", orange = "orange", banana = "banana"}
You would just call back to that value by using t.apple and it would return the same thing as t[1] earlier.
I could go on about tables, indices, and pairs, but it would be too much for me at this hour….
try #9
Posted 19 January 2013 - 06:15 PM
Newbies should Web-Search for Lua Loops tutorial.

"for" loops have this structure:

for blahblahblah do
this_step
and_this_step
repeatedly
end

The 'end' represents the end of the steps-to-repeat list.
The 'do' represents the list's beginning.

The loop will perform all the steps from do to end
then it will update&test the requirements in blahblahblah.
theoriginalbit #10
Posted 19 January 2013 - 06:20 PM
Newbies should Web-Search for Lua Loops tutorial.

"for" loops have this structure:

for blahblahblah do
this_step
and_this_step
repeatedly
end

The 'end' represents the end of the steps-to-repeat list.
The 'do' represents the list's beginning.

The loop will perform all the steps from do to end
then it will update&test the requirements in blahblahblah.
If you actually read the original post you would see that he is in fact working through this webpage http://lua-users.org/wiki/TablesTutorial
Also the 'tutorial' you supplied there explaining for loops has NOTHING to do with the question at hand. You described a sequential for loop not a generic for loop… Maybe you need to do a web-search for "Generic For Loops" and maybe even with the keyword "Lua" added…
Cranium #11
Posted 19 January 2013 - 06:25 PM
Newbies should Web-Search for Lua Loops tutorial.

"for" loops have this structure:

for blahblahblah do
this_step
and_this_step
repeatedly
end

The 'end' represents the end of the steps-to-repeat list.
The 'do' represents the list's beginning.

The loop will perform all the steps from do to end
then it will update&test the requirements in blahblahblah.
That is not even close to what the OP was asking. I believe they are asking about the pairs/ipairs part about it.
theoriginalbit #12
Posted 19 January 2013 - 06:26 PM
That is not even close to what the OP was asking. I believe they are asking about the pairs/ipairs part about it.
Yes they are asking about Generic For Loops not standard For Loops…
ChunLing #13
Posted 20 January 2013 - 10:14 AM
If we're just posting links, to really understand the generic for loop you need to read Chapter 7. If you find it a little difficult to understand…well, that's probably normal.

If you're looking to understand ipairs() don't bother. It isn't a very useful function and will be deprecated (as it's uses can be easily replaced by numeric for loops, as described above).

The pairs() function is a different story, it's very useful.
"for k,v in pairs(t) do…end" basically means that for each key/value pair in the table t, we'll do something. It could be anything. But we'll do it for each and every non-nil key/value pair that is in the table, so the table doesn't have to be sequential.
theoriginalbit #14
Posted 20 January 2013 - 11:04 AM
If we're just posting links -snip-
No one is "just posting links" actually other than the OP link no one even posted any links…
ChunLing #15
Posted 20 January 2013 - 04:55 PM
Umm…. :unsure:/>
Zambonie #16
Posted 21 January 2013 - 04:02 PM
So,I accutly kinda get it :)/> .Although,I proboly should ask this when I know a little more like is.pullevent or something.all it takes is just a little posts… :D/>
Zambonie #17
Posted 21 January 2013 - 04:03 PM
So,I accutly kinda get it :)/> .Although,I proboly should ask this when I know a little more like is.pullevent or something.all it takes is just a little posts… :D/>