powershell - 如何调用嵌套哈希表中的键?

标签 powershell nested hashtable

我目前正在研究 PowerShell 中的哈希表,我了解到变量既可以用作键也可以用作值。此时我已经创建了一个哈希表,并想看看如何将其嵌套在另一个哈希表中。因此,这是我正在做的事情的信息:

我创建了一个名为$avatar的哈希表。其中包含键 “Episode 1”“Episode 2”“Episode 3” 等以及每集的名称作为值。

$avatar = [ordered]@{
      "Episode 1" = "The Boy in the Iceberg";
      "Episode 2" = "The Avatar Returns";
      "Episode 3" = "The Southern Air Temple";
}

然后我想将此哈希表放入另一个名为 $shows 的哈希表中。

$shows = [ordered]@{
      "Avatar" = $avatar;
}

所以,这是我的问题。我是否正确编写了嵌套哈希表的语法?如果不是的话应该怎么写呢?另外,从嵌套哈希表中调用特定键所需的语法是什么?

最佳答案

这很有趣,并且只需研究每个小部分就可以教会您很多东西。

所以我们知道我们有第一个哈希表,它由“键”和“值”组成

$avatar.keys

Episode 1
Episode 2
Episode 3

$avatar.values

The Boy in the Iceberg
The Avatar Returns
The Southern Air Temple

如果要循环每个名称/值对,请使用 .GetEnumerator()

$avatar.GetEnumerator() | ForEach-Object {
    "Name: $($_.name)"
    "Value: $($_.value)"
}

Name: Episode 1
Value: The Boy in the Iceberg
Name: Episode 2
Value: The Avatar Returns
Name: Episode 3
Value: The Southern Air Temple

您可以使用每个键并循环它们以一次对一个键进行操作

$shows.Keys | ForEach-Object {
    $shows.$_
}

Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
Episode 1                      The Boy in the Iceberg                                                                                                                                         
Episode 2                      The Avatar Returns                                                                                                                                             
Episode 3                      The Southern Air Temple 

当您开始嵌套时,只需知道您必须剥开每一层即可。

$shows.Keys | ForEach-Object {
    $shows.$_.GetEnumerator()
}

Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
Episode 1                      The Boy in the Iceberg                                                                                                                                         
Episode 2                      The Avatar Returns                                                                                                                                             
Episode 3                      The Southern Air Temple  

$shows.Keys | ForEach-Object {
    foreach($key in $shows.$_.keys){
        $shows.$_.$key
    }
}

The Boy in the Iceberg
The Avatar Returns
The Southern Air Temple

或者很多不同的方式

关于powershell - 如何调用嵌套哈希表中的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69667842/

相关文章:

Powershell ZIP CopyHere 抵消异步行为

无法识别 Powershell 导入的模块命令

sql - 嵌套 SQL 语句但在内部语句中重用变量?

javascript - 改进了在 javascript 中构建嵌套唯一值数组的方法

javascript - 了解 Dean Edwards 的附加事件 JavaScript

objective-c - cocoa :带枚举键的字典?

windows - 禁用 Ansible 的 Powershell 编码

powershell - 带有通配符的奇怪Powershell GCI递归结果

javascript - 将特定 div 从嵌套 div 中移出

c - C 中的哈希函数给出负数