Jump to content




Iterate in reverse order through a table.


  • You cannot reply to this topic
2 replies to this topic

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 07 December 2012 - 08:36 AM

I want to use a for loop, but instead of iterating sequentially in ascending order, I would like to go in descending order. So for example,
for i = 5,1 do
	print("descending order #"..i)
end
How would I do that?

EDIT: Extensive googling gave me my answer.

for i = 5,1,-1 do --using iterations of -1
	print("descending order #"..i)
end

Edited by Cranium, 07 December 2012 - 08:41 AM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 07 December 2012 - 08:42 AM

for var = start, end, step do

Use a negative value for step.

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 07 December 2012 - 09:09 AM

Literally found the solution only seconds before you posted, Lyqyd. But thanks anyway.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users