ForEach support for arrays

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

ForEach support for arrays

Post 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
Windows 7 & PureBasic 4.4
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

I also think that there's something to do.

but what should be the syntax/behaviour for multidimensionals arrays ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post 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...
Windows 7 & PureBasic 4.4
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post 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
User avatar
macros
User
User
Posts: 88
Joined: Wed Mar 15, 2006 1:47 pm
Location: Munich

Re: ForEach support for arrays

Post 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.
Post Reply