3

I wanted to know what would be the best practice and quickest way to validate a boolean array in PowerShell to see if it's all true respectively all false.

My own approach looks like this:

$boolArray = new-object bool[] 5

0..($boolArray.Length - 1) | ForEach-Object { $boolArray[$_] = $true }

#$boolArray[2] = $false
$boolArray

if (!($boolArray -contains $false)) {
    'All is true.'
}
else {
    'Contains at least one false.'
}

I'm using -contains to validate the array, but no clue if this is really the best way.
I also wanted to know if -contains stops at the first occurrence or always validates the entire array.

6
  • 2
    -contains and -in are the Powershell idiomatic way of doing this just as what you have right now is perfectly fine. Both operators will stop at first occurrence: "These operators stop comparing as soon as they detect the first match..." from learn.microsoft.com/en-us/powershell/module/… Commented Jun 22 at 15:23
  • 2
    These operators also have the negated counterparts, like !($boolArray -contains $false) is the same as $boolArray -notcontains $false and !($false -in $boolArray) is the same as $false -notin $boolArray. Commented Jun 22 at 15:37
  • 1
    Just: if(-not ($boolArray -ne $true)) { ... }, see duplicate: If all values in 'foreach' are true. Commented Jun 22 at 17:12
  • 1
    @iRon that isnt any better than containment operators, its actually worse Commented Jun 22 at 18:18
  • 1
    @SantiagoSquarzon, sorry for my late reaction, I needed some time to let implication of your statement sink in. You're obviously right, but should that really be the case? See: github.com/PowerShell/PowerShell/issues/25720 Commented Jul 2 at 7:52

1 Answer 1

2

-contains and -in are the PowerShell idiomatic way of doing this type of linear comparison, just as what you have right now is perfectly fine and most likely the most efficient way of doing it linearly. Additionally, both operators will stop at first occurrence, see Containment operators:

These operators stop comparing as soon as they detect the first match...

Another notable mention is that these operators have their negated counterparts, meaning, these comparisons equate:

  • !($boolArray -contains $false) same as $boolArray -notcontains $false
  • !($false -in $boolArray) same as $false -notin $boolArray

For collections of bigger size, you may opt for a hash-based lookup, the types you may often see being used for this type of comparison are HashSet<T> and IDictionary implementing types, such as Hash tables.

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Thanks a lot for your detailed answer and your time and efforts!
@burnie glad it was helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.