Answered by
4 years ago Edited 4 years ago
ipairs (a.k.a index-value-pair, or index - pair) has some distinct similarities between pairs.
ipairs
and pairs
are very alike, both of them are used to get pairs of values/elements in a table.
The only difference between the two, is that ipairs and pairs both have different iterators!
How does Ipairs work?
Ipairs
Ipairs only retrieves numerical keys, meaning ipairs can only return values specified in a format.
In addition, if Ipairs
retrieves any value which is non-existent/nil. It will simply stop/break the current loop and finish!
Example:
01 | ListTable = { 8 , 5 , nil , 9 } |
02 | for i,v in ipairs (ListTable) do |
Pairs
Pairs, on the other hand, will run a statement regardless of non-numerical keys (e.g. strings).
This makes this function useful in dictionary or tables with values that contain strings or numbers.
Here's an example of pairs:
01 | local ListDefined = { 2 , 4 , 6 , "hello" , nil } |
02 | for i,v in pairs (ListDefined) do |
References:
pairs v ipairs
Scripting Helpers - What is the difference between pairs and ipairs
(The source I used for this example)
pairs v ipairs - lua users wiki
lua pil
Thanks, JesseSong!
Note: This example very simple for beginners to comprehend, any enquires/inquires should be commented below!