foreach to tell its location

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

foreach to tell its location

Post by Bugala »

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: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: foreach to tell its location

Post by SamuraiCrow »

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.
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: foreach to tell its location

Post by Bugala »

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