Feature request - more flexible EachIn

BlitzMax Forums/BlitzMax Programming/Feature request - more flexible EachIn

ImaginaryHuman(Posted 2005) [#1]
I would like to request that the EachIn command be made a bit more flexible so that it doesn't just go through every single item in an array, but instead lets you define where in the array to start and where to end. (or how many items to do?)

Given that, in code, this command probably gets the array size as a loopcounter, that number could easily be adjusted to something less than the size of the array. Also since it probably maintains a memory address which is incremented, it could easily be initialized to a different location in the array rather than the base of it.

Something like....

For counter=EachIn myarray[7] To 16

or

For counter=EachIn myarray[7] To myarray[16]

Also it would be nice to be able to manually adjust the `loopcounter` from within the loop, like you can with a regular For-Next loop, so that you can jump about in the array if needed, something like:

counter2=0
For counter=EachIn myarray
if counter2=5 then NextIn(5) 'ie skip forward 5 elements
if counter2=15 then PrevIn(5) 'ie skip backward 5 elements
if counter2=10 then DirectIn(2) 'Goto element 2
counter2:+1
next counter

something like that, so you can manipulate where you are in the array? Would be real useful to be able to jump forward, backward, and to any given position.

Anyway, just an idea. Would be useful to me.


skn3(Posted 2005) [#2]
Id like to see eachin become more like the php foreach.

global myicons[10]

for index => indexarray = eachin myitems
    print indexarray[index]
next


The "=> object/variable" is optional.


PowerPC603(Posted 2005) [#3]
If you're using arrays, can't you just access the index directly?

Global MyArray:Int[20]

For index = 5 To 12
	MyArray[index] = Rand(0, 100)
	counter :+ 1
	If counter = 3 then
		index = 7
	EndIf
Next



ImaginaryHuman(Posted 2005) [#4]
Yes of course you can but each time you access the array, Blitz has to take the base address of the array, work out where the offset into the array is based on the index, add it to the base address and then read the array data.

With EachIn, all it seems to have to do, is keep track of where it is in memory, starting at the base address, adding the size of the type to it with each iteration, which is a lot less instructions and it does work faster. For example, a routine I wrote ran at 80 milliseconds with a regular for-next loop using an index, whereas the EachIn technique took only 70 milliseconds. That's something like at 13% speedup.

So yes you can use an index, but it's not as efficient as it could be.