foreach to tell its location

Feature requests for future versions of Hollywood can be voiced here
Post Reply
Bugala
Posts: 676
Joined: Sun Feb 14, 2010 8:11 pm

foreach to tell its location

Post by Bugala » Fri Jul 07, 2017 10:53 pm

I have many times encountered a situation where I have had to change "foreach" loop into "for n=1 to x" loop, because i have been in need of knowing the location.

as example:

Code: Select all

foreach(table, function(key, item)
                                                 if item = searchedthing then removeitem(table, ???)
                                endfunction)
this example is naturally not very good as you shouldnt remove stuff in middle of loop, but just to give you the idea.

For reasons like this, it would be handy to have something like "WhatIndexIsIt()" command which would tell at what index location it is currently going at.

SamuraiCrow
Posts: 146
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: foreach to tell its location

Post by SamuraiCrow » Sat Jul 08, 2017 8:19 pm

What's wrong with the For .. In .. Do syntax? It looks like this:

Code: Select all

For key,item in IPairs(table)
   ...
Next
In this example, key tells where and item tells what.

Bugala
Posts: 676
Joined: Sun Feb 14, 2010 8:11 pm

Re: foreach to tell its location

Post by Bugala » Sat Jul 08, 2017 8:25 pm

ah, thanks. I had forgot that exists. I now rememebr i once before was looking for same problem and either someone suggested or found myself pairs existed, but must have forgot it existed.

Solves the problem.

Post Reply