PowerShell: Member-access enumeration doesn't work with numeric hashtable keys

Prerequisites

Steps to reproduce

Undoubtedly an edge case, but it makes me wonder whether other discrepancies between single-object dot notation (property access) and member-enumeration-based dot notation exist:

@{ 42 = 'foo' }.42           # OK -> 'foo'

(@{ 42 = 'foo1' }, @{ 42 = 'foo2' }).42  -join ', ' # !! No output

Note that the problem does not occur with string-valued hashtable keys (which is what’s typical), e.g. (@{ a42 = 'foo1' }, @{ a42 = 'foo2' }).a42 -join ', ' Similarly, the following works as expected: ([pscustomobject] @{ 42 = 'foo1' }, [pscustomobject] @{ 42 = 'foo2' }).42 -join ', '

Expected behavior

foo
foo1, foo2

Actual behavior

foo

Error details

No response

Environment data

PowerShell Core 7.3.0-preview.4

Visuals

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Great sleuthing, @jhoneill; what an insidious bug (though probably rare in the wild); I’ve taken the liberty of reporting it in #17525

I got it down to this image

It’s almost like the member operator remembers that it has seen a string property “42” causing it to change behaviour - try with and without line 2 😃

$psco = [pscustomobject]@{ 42 = 'foo1' }
$Psco.42
$h = @{ 42 = 'foo1' }
$h.42
$h = (@{ "42" = 'foo1' })
$h.42

Supplementary

  @{  42  = 'foo' }.42      #Returns "foo"
 (@{  42  = 'foo' }).42     #Returns "foo"
@(@{  42  = 'foo' }).42     #Returns nothing
@(@{ "42" = 'foo' }).42     #Returns "foo"

It appears the member operator converts its operand to a string when processing members of an array.