PowerShell嵌套哈希表的正确写法及内部键的调用方法
语法正确性结论
你编写的哈希表嵌套语法完全正确,符合PowerShell的语法规范,将已有哈希表作为值存入另一哈希表的写法无需调整。
嵌套哈希表的调用方法
常用的调用方式有以下几种:
- 点属性调用:是最常用的写法,若键名包含空格,需要用引号包裹键名
示例:$shows.Avatar.'Episode 1',执行后返回值为The Boy in the Iceberg - 方括号索引调用:更适合键名是动态变量的场景
示例:$shows["Avatar"]["Episode 2"],执行后返回值为The Avatar Returns - 两种写法支持混用,例如
$shows.Avatar["Episode 3"]、$shows["Avatar"].'Episode 1'均为合法语法
完整运行验证示例
你可以执行以下代码验证效果:
$avatar = [ordered]@{ "Episode 1" = "The Boy in the Iceberg"; "Episode 2" = "The Avatar Returns"; "Episode 3" = "The Southern Air Temple"; } $shows = [ordered]@{ "Avatar" = $avatar; } # 测试调用 $shows.Avatar.'Episode 1' $shows["Avatar"]["Episode 2"]
内容的提问来源于stack exchange,提问作者Eric Pilati
相关产品推荐
相关产品推荐

