GLBasic forum

Main forum => GLBasic - en => Topic started by: Minion on 2011-Oct-18

Title: FOREACH element number
Post by: Minion on 2011-Oct-18
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 ?
Title: Re: FOREACH element number
Post by: AMateus on 2011-Oct-18
I don't think so... At least I didn't find any.

What is the purpose?

António
Title: Re: FOREACH element number
Post by: ampos on 2011-Oct-18
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:
Title: Re: FOREACH element number
Post by: AMateus on 2011-Oct-18
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
Title: Re: FOREACH element number
Post by: Minion on 2011-Oct-18
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.
Title: Re: FOREACH element number
Post by: 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:

Title: Re: FOREACH element number
Post by: AMateus on 2011-Oct-18
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
Title: Re: FOREACH element number
Post by: Kitty Hello on 2011-Oct-18
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.
Title: Re: FOREACH element number
Post by: matchy on 2011-Oct-19
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

Title: Re: FOREACH element number
Post by: Qube on 2011-Oct-19
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.

Title: Re: FOREACH element number
Post by: MrTAToad on 2011-Oct-19
But then that adds extra code to something that is already present :)
Title: Re: FOREACH element number
Post by: matchy on 2011-Oct-20
 :doubt:
Title: Re: FOREACH element number
Post by: Quentin on 2011-Oct-20
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.
Title: Re: FOREACH element number
Post by: matchy on 2011-Oct-20
It's just a demo comparison really. Besides, how can DIMPUSH a type array?
Title: Re: FOREACH element number
Post by: Minion on 2011-Oct-20
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]
Title: Re: FOREACH element number
Post by: Kitty Hello on 2011-Oct-20
Code (glbasic) Select

FOR i% = 0 TO LEN(mytypes[])-1
   ALIAS reference AS mytypes[i%]

   reference.xy = 1234
NEXT


Almost as fast as the FOREACH. The advantage of FOREACH is, that it won't use the [ ] operator, thus no multiplications. It's just adding to the pointer.