{"id":2344,"date":"2013-12-27T00:01:00","date_gmt":"2013-12-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/27\/phantom-elements-in-a-hash-table\/"},"modified":"2013-12-27T00:01:00","modified_gmt":"2013-12-27T00:01:00","slug":"phantom-elements-in-a-hash-table","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/phantom-elements-in-a-hash-table\/","title":{"rendered":"Phantom Elements in a Hash Table"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft PFE and guest blogger, Chris Wu, talks about working with hash tables.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, <a href=\"\/b\/heyscriptingguy\/archive\/tags\/chris+wu\/\" target=\"_blank\">Chris Wu<\/a>&hellip;<\/p>\n<p>Admittedly, hash tables didn&rsquo;t catch my attention when I started using Windows PowerShell, but over time it has proven to be one of my favorites. This is largely thanks to its ability to assign names to values for fast lookup.<\/p>\n<p>Hash tables store key-value (or name-value) pairs, which means typically we use strings as keys to locate corresponding values. Windows PowerShell also takes keys from the command line as strings. So the following examples will create the same hash table:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-1.png\"><\/a><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-1.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-1.png\" border=\"0\" \/><\/a><\/p>\n<p>However, it is not a restriction, per se. Any type of data can be used as a key in hash tables. The following example uses <b>int<\/b> values as keys, which makes a hash table appears like an array, in terms of syntax:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-2.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-2.png\" border=\"0\" \/><\/a><\/p>\n<p>Other than indexing, hash tables also support dot notation to access elements. However, in Windows PowerShell&nbsp;2.0, dot notation syntax doesn&rsquo;t support non-string keys. This seems to have been improved in Windows PowerShell&nbsp;3.0.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-3.png\"><\/a><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-3.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-3.png\" border=\"0\" \/><\/a><\/p>\n<p>Actually, as long as a hash table is manually crafted, there is no mystery. Everything was plain and straightforward to me, until I was hit by this particular issue where I was trying to get a list of fixed logical disks by accessing a hash table that returned from the <b>Group-Object<\/b> cmdlet with the <b>-AsHashTable<\/b> parameter:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-4.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-4.png\" border=\"0\" \/><\/a><\/p>\n<p>What? No fixed disks? That&rsquo;s impossible! So I double checked:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-5.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-5.png\" border=\"0\" \/><\/a><\/p>\n<p>A key called 3 was indeed in the list. The workaround I tried previously also proved that the element was in the hash table. So what the heck?<\/p>\n<p>I scratched my head hard, until finally I realized that those keys are objects too. So each key has its type as well. Then I investigated:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-6.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-6.png\" border=\"0\" \/><\/a><\/p>\n<p>Here we go&hellip;<\/p>\n<p>The keys were of the unsigned<b> Int32<\/b> type, which are different from the signed <b>Int32<\/b> type that Windows PowerShell uses by default. So when Windows PowerShell searched the hash table, looking for an element for <b>Int32<\/b> value 3, there was no match! When I figured out that, it became apparent how the elements can be accessed:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-7.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-7.png\" border=\"0\" \/><\/a><\/p>\n<p>One take-away lesson that I learned: Keys in a hash table are unique only when the data and the type are considered. I could create a hash table that contain four keys, all sharing the same look:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-8.png\"><img decoding=\"async\" alt=\" \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-27-13-8.png\" border=\"0\" \/><\/a><\/p>\n<p>Now, how do we get each of those elements?<\/p>\n<p>~Chris<\/p>\n<p>Thanks, Chris, for another awesome blog post.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft PFE and guest blogger, Chris Wu, talks about working with hash tables. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, Chris Wu&hellip; Admittedly, hash tables didn&rsquo;t catch my attention when I started using Windows PowerShell, but over time it has proven to be one of my favorites. This is largely [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[18,302,56,3,4,45],"class_list":["post-2344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables-and-dictionary-objects","tag-chris-wu","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft PFE and guest blogger, Chris Wu, talks about working with hash tables. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, Chris Wu&hellip; Admittedly, hash tables didn&rsquo;t catch my attention when I started using Windows PowerShell, but over time it has proven to be one of my favorites. This is largely [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2344"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2344\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}