Page 1 of 1

ForEach support for arrays

Posted: Tue Aug 07, 2007 2:07 pm
by milan1612
Would be cool to be able to compile this:

Code: Select all

Dim arr.l(5)
ForEach i.l In arr()
  Debug i
Next

Posted: Tue Aug 07, 2007 7:15 pm
by Flype
I also think that there's something to do.

but what should be the syntax/behaviour for multidimensionals arrays ?

Posted: Tue Aug 07, 2007 7:20 pm
by milan1612
hmm, good question :?

EDIT: What about that:

Code: Select all

Dim arr.l(5, 2)
ForEach i.l In arr()[0]
  Debug i
Next
Where the [0] is the dimension you want to enumerate...

Posted: Thu Aug 09, 2007 12:59 am
by Ollivier
Like that?

Code: Select all

Macro Each(VarName, ArrayName, Dimension = 0)
;Ollivier 9/8/7
  PrivateLong90 = PeekL(@ArrayName#() - 8)
  *PrivateLong91 = PeekL(@ArrayName#() - 12) + 4
  If PeekL(*PrivateLong91) = 0
    PrivateLong92 = PrivateLong90
  Else
    PrivateLong92 = 1
    PrivateLong93 = 1
    PrivateLong94 = 0  
    For PrivateLong96 = 0 To Dimension
      PrivateLong95 = PrivateLong94
      PrivateLong94 = PeekL(*PrivateLong91)
      PrivateLong92 = PrivateLong90 / PrivateLong94 / PrivateLong93
      PrivateLong93 * PrivateLong92     
      If PrivateLong94 = PrivateLong95: PrivateLong92 = PrivateLong94: EndIf      
      *PrivateLong91 + 4
    Next
  EndIf
  PrivateLongNine#VarName = PrivateLong92 - 1
  For VarName = 0 To PrivateLongNine#VarName
EndMacro
Example:

Code: Select all

Dim Array.S(3, 1, 3, 2, 4)

; Fill all the strings array with "A"
Each(i, Array, 0)
  Each(j, Array, 1)  
    Each(k, Array, 2)
      Each(l, Array, 3)
        Each(m, Array, 4)        
          Array(i, j, k, l, m) = "A"
        Next
      Next
    Next
  Next
Next

Re: ForEach support for arrays

Posted: Wed Feb 06, 2019 4:07 pm
by macros
I know the thread is old, but I would love to have ForEach for Arrays.
It would make writing programs quite more comfortable. And, at least for 1-dimensional arrays, intuitive.

For multidimensional arrays the keyword makes it obvious I think, it should iterate over all dimensions.