20
$a = @() 

How do I check if $a above is empty (which it is). I would like to get $true as answer.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Spencer E
  • 203
  • 1
  • 2
  • 4

2 Answers2

35

That's not an associative array, it's a regular array, but the answer is the same. Use .Count and compare to 0.

An associative array is called a [hashtable] in PowerShell and its literal form uses @{} (curly braces).

@{}.Count -eq 0  # hashtable (associative array)
@().Count -eq 0  # array
briantist
  • 45,546
  • 6
  • 82
  • 127
  • What's the protocol here; do we fix the title of the question? – Charlie Joynt May 05 '16 at 21:52
  • @CharlieJoynt unclear; I don't know if the asker meant hashtable but wrote the syntax wrong, or wrote the correct syntax and the wrong title. I'm inclined to leave it for now unless the poster clarifies. – briantist May 06 '16 at 00:43
  • 1
    Trap for Young Players: Since PowerShell 3 introduced the synthetic Length property, I had assumed that I could always use Length and that any object with a Count property would have Count = Length. Not so for empty hash tables: `@{}.Length` is 1 while `@{}.Count` is 0. – Simon Elms Nov 07 '17 at 12:00
  • 1
    @SimonTewsi yes, v3 added synthetic properties for both `.Count` and `.Length`, and they are equal, but they never override an existing property with the same name. So as a counter to your example, in a string it would be flipped: `"".Count` is 1, `"".Length` is 0. `(5).Count` and `(5).Length` are both 1. The purpose of the synthetic properties is so that you don't need to use conditionals to determine whether a variable contains an array or a single value. It makes a lot of patterns easier. – briantist Nov 07 '17 at 15:55
  • @briantist: So the synthetic Count and Length properties really have just a single use-case, to determine whether a variable is an array or a single value? And they're pretty much useless for anything else (eg string.Count and HashTable.Length suggest it would be dangerous to universally check Count and Length for variables of unknown data type)? – Simon Elms Nov 07 '17 at 20:49
  • @SimonTewsi maybe.. but what other use case is there for checking for `Count` or `Length` on a variable of unknown type? – briantist Nov 07 '17 at 21:04
  • @briantist: I was thinking in terms of Python, another dynamically typed language, where users are encouraged to make scripts and functions as type-agnostic as possible. So I was trying to do the same in PowerShell. Specifically, I was assuming that `Count` or `Length`, being universal properties, could be used to test for "emptiness" of any object (eg string, array, hash table). However, given that `Count` is fixed at 1 for strings and `Length` is fixed at 1 for hash tables, that wouldn't work. – Simon Elms Nov 07 '17 at 21:20
  • @SimonTewsi ah I see. PowerShell is not exactly dynamically typed.. it's a bit strange as it's a .Net language but the script engine wraps every object in a type called `PSObject` and then whenever possible makes that transparent to you (sort of). Anyway, testing for emptiness, or let's say **falsiness** is fairly easy: just use it as a boolean: `if ($object) {}`. If object is: null, 0, an empty string, or an empty array, it coalesces to `$false`. It doesn't work on a hashtable though; there's no fully universal way; you will have to know at least a little bit about the type you were expecting – briantist Nov 07 '17 at 22:51
  • @briantist: I'd come to the conclusion that hash tables would prevent a universal check for emptiness. Thanks for confirming. And thanks for the additional info about the PSObject wrapper. – Simon Elms Nov 07 '17 at 23:01
6

Arrays have Count property, and you can check if this value is 0. So the condition you would check for is

$a.Count -eq 0
dotnetom
  • 24,551
  • 9
  • 51
  • 54