Title: Write-Output -NoEnumerate doesn't handle Hashtable properly · Issue #14047 · PowerShell/PowerShell · GitHub
Open Graph Title: Write-Output -NoEnumerate doesn't handle Hashtable properly · Issue #14047 · PowerShell/PowerShell
X Title: Write-Output -NoEnumerate doesn't handle Hashtable properly · Issue #14047 · PowerShell/PowerShell
Description: Steps to reproduce $g = @{} $g["foo"] = 1 $g["bar"] = 1 $g["foo"] # returns 1 $g."foo" # returns 1 $h = write-output $g -NoEnumerate $h["foo"] # returns $null $h."foo" # returns 1 $h["bar"] # returns $null $h."bar" # returns 1 $h.count #...
Open Graph Description: Steps to reproduce $g = @{} $g["foo"] = 1 $g["bar"] = 1 $g["foo"] # returns 1 $g."foo" # returns 1 $h = write-output $g -NoEnumerate $h["foo"] # returns $null $h."foo" # returns 1 $h["bar"] # retur...
X Description: Steps to reproduce $g = @{} $g["foo"] = 1 $g["bar"] = 1 $g["foo"] # returns 1 $g."foo" # returns 1 $h = write-output $g -NoEnumerate $h["foo"] # re...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/14047
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Write-Output -NoEnumerate doesn't handle Hashtable properly","articleBody":"## Steps to reproduce\r\n```\r\n$g = @{}\r\n$g[\"foo\"] = 1\r\n$g[\"bar\"] = 1\r\n$g[\"foo\"] # returns 1\r\n$g.\"foo\" # returns 1\r\n\r\n$h = write-output $g -NoEnumerate\r\n$h[\"foo\"] # returns $null\r\n$h.\"foo\" # returns 1\r\n$h[\"bar\"] # returns $null\r\n$h.\"bar\" # returns 1\r\n$h.count # returns 1\r\n$($h.keys | measure).count # returns 2\r\n\r\n$f = write-output $g\r\n$f[\"foo\"] # returns 1\r\n$f.\"foo\" # returns 1\r\n$f[\"bar\"] # returns 1\r\n$f.\"bar\" # returns 1\r\n$f.count # returns 2\r\n```\r\n\r\n## Expected behavior\r\n\r\n``$h[\"foo\"] # returns 1``\r\n``$h.count # returns 2``\r\n\r\n## Actual behavior\r\n\r\n``$h[\"foo\"] # returns $null``\r\n``$h.count # returns 1``\r\n\r\n## Environment data\r\n```\r\nName Value\r\n---- -----\r\nPSVersion 7.1.0\r\nPSEdition Core\r\nGitCommitId 7.1.0\r\nOS Microsoft Windows 10.0.19042\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n\r\n## Notes\r\nI thought I would use `end { Write-Output $Result -NoEnumerate }` to be safe for `ConvertTo-Dictionary` the way I do to ensure an empty HashSet is returned by `ConvertTo-HashSet` when pipeline input is empty, but it turns out you must use `end { $Result }` or you get back a broken Hashtable. \r\n\r\nMaybe this is expected behavior somehow, but I don't understand what $h is and how $h.\"foo\" and $h.\"bar\" both work even though $h.count is 1. And why $h.keys gives 2 items, while $h.count is 1. I guess $h is being unrolled and that is how it is working. Still, not what I would have expected from -NoEnumerate. \r\n```\r\n\u003c#\r\n.SYNOPSIS\r\nConvert a stream of objects to a Dictionary\r\n.EXAMPLE\r\n$services = Get-Service | ConvertTo-Dictionary {$_.Name} {$_.DisplayName}\r\n#>\r\nfunction ConvertTo-Dictionary {\r\n param(\r\n [ScriptBlock]$KeyExpression,\r\n [ScriptBlock]$ValueExpression = { $_ }\r\n )\r\n begin { $Result = @{} }\r\n process { $Result[(&$KeyExpression)] = (&$ValueExpression) }\r\n end { $Result } \r\n}\r\n\r\n\u003c#\r\n.SYNOPSIS\r\nConvert a stream of objects to a HashSet\r\n.EXAMPLE\r\n$services = get-service | select -expandproperty Name -first 10 | ConvertTo-HashSet \r\n$null -eq $services # False\r\n$Services = get-service | select -expandproperty Name -first 0 | ConvertTo-HashSet \r\n$null -eq $services # False\r\n#>\r\nfunction ConvertTo-HashSet {\r\n begin { $hashSet = [System.Collections.Generic.HashSet[String]] @() }\r\n process {\r\n $hashSet.add($_) | Out-Null\r\n }\r\n end { Write-Output -NoEnumerate $hashSet }\r\n}\r\n```","author":{"url":"https://github.com/kganjam","@type":"Person","name":"kganjam"},"datePublished":"2020-11-12T04:04:33Z","url":"https://github.com/PowerShell/PowerShell/issues/14047"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:98438f54-c3c1-3588-0b77-78ca946165e0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1F6:249BAE:2EB908:408D87:6AFD6552 |
| html-safe-nonce | 73593137aab2c50e6d802c45f7f30e82ebc22b22eace6dce7cbe88589162bd1c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUY2OjI0OUJBRToyRUI5MDg6NDA4RDg3OjZBRkQ2NTUyIiwidmlzaXRvcl9pZCI6IjgxOTI4MzY0MjYwMDYzNTczMzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6dd4c8c352853d0c02e1ee5bc084c2ee5ff8abda88aaf84a71c930f91cc0aea2 |
| hovercard-subject-tag | issue:741259679 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/14047/issue_layout |
| twitter:image | https://opengraph.githubassets.com/905ab0716926c8c3b8caf2004c4e15e89cd93643e1b11c5b2aa6b176e2299c3d/PowerShell/PowerShell/issues/14047 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/905ab0716926c8c3b8caf2004c4e15e89cd93643e1b11c5b2aa6b176e2299c3d/PowerShell/PowerShell/issues/14047 |
| og:image:alt | Steps to reproduce $g = @{} $g["foo"] = 1 $g["bar"] = 1 $g["foo"] # returns 1 $g."foo" # returns 1 $h = write-output $g -NoEnumerate $h["foo"] # returns $null $h."foo" # returns 1 $h["bar"] # retur... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kganjam |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42a67d7c7488271ca8d7e6dc3942424e6de3bff43b1ed4a9c6f0e1a69b73b389 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5c3779ac05f3590d8d7ea0c02b14e678ae8179ae |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width