格式化嵌套的哈希表 [英] Format a nested hashtable
本文介绍了格式化嵌套的哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个包含异类数据的哈希表,例如:
Given a hashtable that contains heterogeneous data such as:
$items = @{
a = @{a1 = "A1"; a2 = "A2"; a3 = "A3" }
b = 1234
c = @{c1 = "C1"; c2 = "C2"; c3 = "C3" }
d = [DateTime]::Now
}
当我尝试使用以下内容显示内容时:
When I attempt to display the contents using the following:
$items | Format-Table -AutoSize
输出为:
Name Value
---- -----
c {c3, c1, c2}
d 05/23/15 11:37:56
b 1234
a {a2, a3, a1}
但是如何扩展嵌套哈希表的内容,以便可以看到诸如以下的键值对:
But how can I expand the contents of the nested hashtables so that I can see the key-value pairs such as:
Name Value
---- -----
c {c3=C3, c1=C1, c2=C2}
d 05/23/15 11:37:56
b 1234
a {a2=A2, a3=A3, a1=A1}
嵌套键/值对的确切显示格式不是非常关键,我只想看看它们.
The exact display format of the nested key-value pairs is not super critical, I just want to see them.
推荐答案
您需要自己扩展嵌套的哈希表:
You need to expand nested hashtables yourself:
$items | Format-Table Name, @{n='Value';e={
if ($_.Value -is [Hashtable]) {
$ht = $_.Value
$a = $ht.keys | sort | % { '{0}={1}' -f $_, $ht[$_] }
'{{{0}}}' -f ($a -join ', ')
} else {
$_.Value
}
}}
这篇关于格式化嵌套的哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文