How to check if an associative array is empty in powershell

风流意气都作罢 提交于 2019-12-08 15:55:59

问题


$a = @() 

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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/37058338/how-to-check-if-an-associative-array-is-empty-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!