我目前正在 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;
}
所以,這是我的問題。我是否正確撰寫了嵌套哈希表的語法?如果不是,應該怎么寫?另外,從嵌套哈希表中呼叫特定鍵所需的語法是什么?
uj5u.com熱心網友回復:
這很有趣,并且可以教你很多東西,只是調查每個小部分。
所以我們知道我們有第一個哈希表,它由“鍵”和“值”組成
$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
或者很多不同的方式
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331889.html
