I’ve been following the buzz about the PowerShell Scripting Games on Twitter, and came across this PowerShell brain teaser from beefycode.
Here’s my answer, wish me luck!
( $a -eq $b ) -ne ( $b -eq $a )
The key to this puzzle can be found in PowerShell help:
When the input to an operator is a scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values. If there are no matches in a collection, comparison operators do not return anything.
( $a -eq $b ) -ne ( $b -eq $a ), like so:
PS C:\Users\Dave> $a = 1
PS C:\Users\Dave> $b = 1,2
PS C:\Users\Dave> $a.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
PS C:\Users\Dave> $b.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\Users\Dave> $a -eq $b
False
PS C:\Users\Dave> $b -eq $a
1
PS C:\Users\Dave> ( $a -eq $b ) -ne ( $b -eq $a )
True
Any questions?