Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Powershell how can I check if all items from one array exist in a second array?

So let's say I have this array:

$requiredFruit= @("apple","pear","nectarine","grape")

And I'm given a second array called $fruitIHave. How can I check that $fruitIHave has everything in $requiredFruit. It doesn't matter if there are more items in $fruitIHave just as long as everything in $requiredFruit is there.

I know I could just iterate over the list, but that seems inefficient, is there a built-in method for doing this?

like image 386
Glenn Slaven Avatar asked Mar 22 '12 01:03

Glenn Slaven


1 Answers

Do you try Compare-Object :

$requiredFruit= @("apple","pear","nectarine","grape")
$HaveFruit= @("apple","pin","nectarine","grape")
Compare-Object $requiredFruit $haveFruit
InputObject                                                 SideIndicator
-----------                                                 -------------
pin                                                         =>
pear                                                        <=

Compare-Object $requiredFruit $haveFruit | where {$_.sideindicator -eq "<="} | % {$_.inputobject}
pear
like image 112
JPBlanc Avatar answered Oct 17 '22 04:10

JPBlanc