# For Each True in Boolean Array

hi peeps i hope this finds you well
any idea why you can’t do this or how to get around it without using a string a just true false value
dim Y(6) as Boolean
Y(1) = True
y(3) = true
Y(5) = True
y(6) = true

For each True as Boolean in Y
msgbox str®
next

cheers clef

[quote=135440:@James Cleverly]hi peeps i hope this finds you well
any idea why you can’t do this or how to get around it without using a string a just true false value
dim Y(6) as Boolean
Y(1) = True
y(3) = true
Y(5) = True
y(6) = true

For each True as Boolean in Y
msgbox str®
next

cheers clef[/quote]

Don’t use the value directly. Use a variable :

[code] dim Y(6) as Boolean
Y(1) = True
y(3) = true
Y(5) = True
y(6) = true

For each boo as boolean in Y
msgbox str(boo)
next[/code]

Hi there but it also look as the ones that are false !
just wondering if there is way to do nicely more performance without the if statement on the next line

One way or another, the value has to be tested so you’ll need an If. If the order matters, you should not use For Each either.

[quote=135446:@James Cleverly]Hi there but it also look as the ones that are false !
just wondering if there is way to do nicely more performance without the if statement on the next line[/quote]

For such an elementary code, why do you worry about performance so much you want to dispose of an if statement ? As a general rule, you should start by writing code that works, and only afterward, optimize what needs faster execution…

This certainly does not seem to qualify. Booleans and for each are inherently faster than, say, intense calculation.

Michel you mean like

Dim Y(6) As Boolean
y(6) = True //y won’t have y(6)

James, even if you found a way somewhere whether you see it or not a text for True or False would have to take place. The another option you have is make an integer array and only have the index of the True values in the array.

Dim Y() As Integer
y.Append 1
y.append 3
y.append 6

[code] dim Y(6) as Boolean
Y(1) = True
y(3) = true
Y(5) = True
y(6) = true

For each boo as boolean in Y
if boo then msgbox str(boo)
next[/code]

The general form is

[quote]for each element as type in array of type[/quote] (this is close enough for this discussion)

But what you’re trying is to get the FOR EACH to locate each TRUE value in the array
Or at least that seems to be what you’re expecting
All FOR EACH does is iterate over the elements - not their values

So to find every ELEMENT that has the VALUE true you need to write the code to check whatever value it is you’re interested in

[quote] if element is true then
msgbox str® // not sure where r comes from
end if [/quote]