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 ?
I don't think so... At least I didn't find any.
What is the purpose?
António
local n=0
foreach v in var[]
do_your_stuff
inc n
next
:nana: :nana: :nana: :nana: :nana: :nana: :nana: :nana: :nana:
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)
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
I just went with the alternate version
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.
Quote from: AMateus on 2011-Oct-18
And then DELETE command is used and the app crashes =D
if youwanttodelete=true
dec n
delete v
endif
:nana: :nana: :nana: :nana: :nana:
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
if youwanttodelete=true
dec n
delete v
endif
:nana: :nana: :nana: :nana: :nana:
ahahahahah
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.
I use an id each time I add a type array for id reference or even sorting.
TYPE _array
value // first item is sorted on
id
ENDTYPE
GLOBAL array[] as _array
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.
But then that adds extra code to something that is already present :)
:doubt:
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.
It's just a demo comparison really. Besides, how can DIMPUSH a type array?
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]
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.