locked
hashtable or not? RRS feed

  • Question

  • can anyone explain this behaviour:

    [working]

    PS C:\Temp> $h2=@{'a'=1;'b'=2}
    PS C:\Temp> $h2.keys
    a
    b
    PS C:\Temp> $h2.ContainsKey('a')
    True

    [not working /: ]

    PS C:\Temp> $h3='a','b'|group -AsHashTable
    PS C:\Temp> $h3

    Name                           Value
    ----                           -----
    b                              {b}
    a                              {a}


    PS C:\Temp> $h3.ContainsKey('a')
    False


    -o((: Leliv


    • Edited by Lelivienne Friday, February 16, 2018 1:55 PM fix
    Friday, February 16, 2018 1:54 PM

Answers

  • Here is how grouping works:

    PS D:\scripts> $o = @(
    >>     [pscustomobject]@{N='a';V=10},
    >>     [pscustomobject]@{N='b';V=20}
    >> )
    PS D:\scripts> $o|group N
    
    Count Name                      Group
    ----- ----                      -----
        1 a                         {@{N=a; V=10}}
        1 b                         {@{N=b; V=20}}
    
    
    PS D:\scripts> $o|group N -AsHashTable
    
    Name                           Value
    ----                           -----
    a                              {@{N=a; V=10}}
    b                              {@{N=b; V=20}}
    
    
    PS D:\scripts> $h = $o|group N -AsHashTable
    PS D:\scripts> $h.ContainsKey('a')
    True
    PS D:\scripts>
    


    \_(ツ)_/

    • Proposed as answer by Richard MuellerMVP Friday, February 16, 2018 3:11 PM
    • Marked as answer by Lelivienne Monday, February 19, 2018 8:57 AM
    Friday, February 16, 2018 2:15 PM
  • Hi Leliv,

    Based on my research, you can use Contains method directly for an array:
    $h3 = 'a', 'b'
    $h3.Contains('a')

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Lelivienne Monday, February 19, 2018 8:57 AM
    Monday, February 19, 2018 7:02 AM

All replies

  • Grouping only works correctly with objects that have properties.


    \_(ツ)_/

    Friday, February 16, 2018 2:10 PM
  • Here is how grouping works:

    PS D:\scripts> $o = @(
    >>     [pscustomobject]@{N='a';V=10},
    >>     [pscustomobject]@{N='b';V=20}
    >> )
    PS D:\scripts> $o|group N
    
    Count Name                      Group
    ----- ----                      -----
        1 a                         {@{N=a; V=10}}
        1 b                         {@{N=b; V=20}}
    
    
    PS D:\scripts> $o|group N -AsHashTable
    
    Name                           Value
    ----                           -----
    a                              {@{N=a; V=10}}
    b                              {@{N=b; V=20}}
    
    
    PS D:\scripts> $h = $o|group N -AsHashTable
    PS D:\scripts> $h.ContainsKey('a')
    True
    PS D:\scripts>
    


    \_(ツ)_/

    • Proposed as answer by Richard MuellerMVP Friday, February 16, 2018 3:11 PM
    • Marked as answer by Lelivienne Monday, February 19, 2018 8:57 AM
    Friday, February 16, 2018 2:15 PM
  • i understand how it works when it works. and i empirically confirm it is not working when converting from array...

    what i don't get is the fact, that if you have out hashtable, not knowing how it was created, it looks ... normally. and still doesn't work /:

    PS C:\Temp> $h3|gm


       TypeName: System.Collections.Hashtable

    [...]

    PS C:\Temp> $h3

    Name                           Value
    ----                           -----
    b                              {b}
    a                              {a}

    so why is containsKey return false? what's different about this object?


    -o((: Leliv

    Friday, February 16, 2018 3:19 PM
  • Hi Leliv,

    Based on my research, you can use Contains method directly for an array:
    $h3 = 'a', 'b'
    $h3.Contains('a')

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Lelivienne Monday, February 19, 2018 8:57 AM
    Monday, February 19, 2018 7:02 AM
  • Hi guys!

    although this HT object stays a mistery to me, you gave me practical tricks and usage, which will be helpful.

    thx for your help!


    -o((: Leliv

    Monday, February 19, 2018 8:59 AM