lara400
lara400

Reputation: 4736

How to match three or more items

Can't seem to get my head around this. I want to compare three (or more) items - example below - how can I get it to state true? If I do two items it works fine

$a = 2
$b = 2
$c = 2

$a -match $b -match $c
False

Looking at $Matches it only contains two items. I tried brackets around $a and $b but still get the same thing - it keeps on only looking at the first two and ignores the third.

PS C:\Windows\system32> $Matches

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
0                              2      

Upvotes: 1

Views: 112

Answers (3)

Prerna Jain
Prerna Jain

Reputation: 1250

Your snippet is not working as expected because :

comparing two variables in the first place, gives you true or false, that means 1 or 0. So, if you'll compare 1 or 0 with 2, it will obviously give false.

In simple terms :

$a -match $b -match $c equates to :

$a -match $b 
true

true -match $c
false

So, as Martin has answered, you need to do it this way if you need it for regular expression comparison:

$a -match $b -and $a -match $c
true

But since you are comparing values so you need to use, -eq .

Upvotes: 2

Joey
Joey

Reputation: 354834

$a -match $b -match $c

actually results in the following:

[bool] $r1 = $a -match $b
[string] $r1 -match $c

Which is probably not what you want. In fact, I'm quite unsure what you actually want. The -match operator performs a regex match of the left operand. Do you perhaps mean something like

$a -eq $b -and $b -eq $c

?

Upvotes: 1

Martin Brandl
Martin Brandl

Reputation: 59031

You need to combine your checks using -and. Also use -eq (equals) here:

$a -eq $b -and $a -eq $c

Upvotes: 0

Related Questions