Search code examples
powershellnestedhashtable

How to Call Keys in Nested Hash Tables?


I'm studying Hash Tables in PowerShell at the moment and I learned that variables can be used as both keys and values. I had already created a hash table by this point and wanted to see how I could nest that within another hash table. So, here's the information for what I'm doing:

I created a hash table named $avatar. Within this are the keys "Episode 1", "Episode 2", "Episode 3", etc. along with the name of each episode as the value.

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

So then I wanted to put this hash table within another with the name $shows.

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

So, here's my question. Did I write the syntax for nesting the hash table correctly? If not, how should it be written? Also, what is the syntax required for calling a specific key from the nested hash table?


Solution

  • It's fun and can teach you a lot just to investigate each little part.

    So we know we have the first hash table, it's made up of "keys" and "values"

    $avatar.keys
    
    Episode 1
    Episode 2
    Episode 3
    
    $avatar.values
    
    The Boy in the Iceberg
    The Avatar Returns
    The Southern Air Temple
    

    If you want to loop over each name/value pair, use .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
    

    You can take each key and loop over them to act on one at a time

    $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 
    

    When you get into nesting, just know you will have to peel back each layer.

    $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  
    

    or

    $shows.Keys | ForEach-Object {
        foreach($key in $shows.$_.keys){
            $shows.$_.$key
        }
    }
    
    The Boy in the Iceberg
    The Avatar Returns
    The Southern Air Temple
    

    Or lots of different ways