【问题标题】:How to Call Keys in Nested Hash Tables?如何调用嵌套哈希表中的键?
【发布时间】:2021-12-08 13:33:22
【问题描述】:

我目前正在研究 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;
}

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

【问题讨论】:

  • 是的,它看起来不错。您是否检查了 $shows 变量以查看它是否包含您所期望的内容?
  • @DougMauer 我刚刚做了。它显示键的名称,但我看不出它是否还包含值。
  • 例如$shows.Avatar.'Episode 2'?
  • $shows.Values 应该显示所有的键和值
  • @JosefZ,是的。我之前尝试过这种确切的语法,但它第一次抛出了一个错误。也许我第一次有点不对劲。谢谢。

标签: powershell nested hashtable


【解决方案1】:

这很有趣,可以教你很多,只是为了调查每个小部分。

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

$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

或者很多不同的方式

【讨论】:

  • 一个非常彻底的答案。太感谢了。你最后还教了我一堆我不知道的功能和用途,所以更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2012-03-09
相关资源
最近更新 更多