Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an empty list instead of null?

Tags:

powershell

Suppose I have a couple of arrays:

$a = 1,2,3
$b = 4,5,6

and I get the intersection (empty):

$c = $a |?{$b -contains $_}

I would expect $c to contain an empty list such that I could do:

$c.length

but I don't. I get a $null (which issues an exception when queried for .length). How can I get an empty list back instead?

like image 829
ekkis Avatar asked Oct 24 '25 14:10

ekkis


1 Answers

Wrap it in the array constructor:

$c = @($a | ? { $b -contains $_ })
like image 169
Andy Lamb Avatar answered Oct 27 '25 08:10

Andy Lamb