FOREACH element number

Previous topic - Next topic

Minion

During a FOREACH loop, is there anyway of knowing wich element number the ref is pointing too ? I have had a gander thru the help file and can`t seem to find anything (but I may have just missed it). Is it possible or am I clucthing at straws ?

AMateus

I don't think so... At least I didn't find any.

What is the purpose?

António

ampos

Code (glbasic) Select
local n=0

foreach v in var[]
   do_your_stuff
   inc n
next


:nana: :nana: :nana: :nana: :nana: :nana: :nana: :nana: :nana:
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

AMateus

And then DELETE command is used and the app crashes =D

Can we do like this? I'm not sure (i mean, compare ref with the i element)

Code (glbasic) Select

foreach v in var[]
   do_your_stuff
   print GetRefId(v), 0, 0
next

function GetRefId: ref
    FOR i = 0 to BOUNDS(var[],0)-1
    if ref = var[i]
        return i
    NEXT
endfunction

Minion

I just went with the alternate version

Code (glbasic) Select

FOR i = 0 to BOUNDS(field[],0)-1
   ref = field[i]
   ...
   field[i] = ref
NEXT


I have certain elemnts reacting with other elements (controlling flight paths etc) so i can link directly between 2 (or more) elements.

ampos

Quote from: AMateus on 2011-Oct-18
And then DELETE command is used and the app crashes =D


Code (glbasic) Select
if youwanttodelete=true
   dec n
   delete v
endif


:nana: :nana: :nana: :nana: :nana:

check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

AMateus

Quote from: ampos on 2011-Oct-18
Quote from: AMateus on 2011-Oct-18
And then DELETE command is used and the app crashes =D


Code (glbasic) Select
if youwanttodelete=true
   dec n
   delete v
endif


:nana: :nana: :nana: :nana: :nana:

ahahahahah

Kitty Hello

Yes, it's pretty much a problem.
I think I do have the index in C++, so it would be just a matter of getting a handle to it. I'll have a look. It's pretty hard because that's so much special syntax that has to be done here. Sort of meta-programming. The reference to foo must call the integer foo_foreach_index (or what it's called).
A #define macro can do that nicely, but if you have a typo the compiler errors are bad.

matchy

#8
I use an id each time I add a type array for id reference or even sorting.

Code (glbasic) Select

TYPE _array
value // first item is sorted on
id
ENDTYPE

GLOBAL array[] as _array


Qube

I don't think it's worth messing with the FOREACH coding, especially as people have already said, you can (as I do when needed) use a myCoolType.id% variable to mark the index or use the BOUNDS() command.


MrTAToad

But then that adds extra code to something that is already present :)

matchy

#11
 :doubt:

Quentin

Hello matchy,

what is it good for? We have already commands DIMPUSH and DIMDEL.

Regarding FOREACH:
please remember there is still the "normal" FOR loop. If you really need an index, use this one. But please don't change FOREACH or make it more complex. This command is wonderful easy and fast. Don't kill this advantage.

matchy

It's just a demo comparison really. Besides, how can DIMPUSH a type array?

Minion

Quote from: Quentin on 2011-Oct-20
Regarding FOREACH:
please remember there is still the "normal" FOR loop. If you really need an index, use this one. But please don't change FOREACH or make it more complex. This command is wonderful easy and fast. Don't kill this advantage.

Spot on !

[+1]