none
Array or Hashtable? RRS feed

  • Question

  • Hello everyone,

    Let's say I have a file somewhere with following content:

    abc=1
    cde=2
    def=3

    And I have an empty Hashtable:

    $ht1 = @{}

    Now, I read file content and convert it to another Hashtable, like this:

    $ht2 = Get-Content -Path .\file.txt | ConvertFrom-StringData

    If I try to add $ht2 to $ht1 I get following error:

    PS > $ht1 += $ht2
    A hash table can only be added to another hash table.
    At line:1 char:1
    + $ht1 += $ht2
    + ~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : AddHashTableToNonHashTable

    Hmmm, but both objects are Hashtables, aren't they? Let's check:

    PS > $ht1 | Get-Member
    
       TypeName: System.Collections.Hashtable
    ...
    
    PS > $ht2 | Get-Member
    
       TypeName: System.Collections.Hashtable
    ...

    They are... or are they? Let's check another way:

    PS > $ht1.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Hashtable                                System.Object
    
    PS > $ht2.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array

    Aha! There is the difference. But the documentation for ConvertFrom-StringData cmdlet says that it "Converts a string containing one or more key and value pairs to a hash table."

    I am confused, am I missing something?

    So, the questions are:

    What is the real object type of $ht2?
    Is there an error somewhere in ConvertFrom-StringData cmdlet behavior or documentation?
    How do I add contents of the file.txt to existing Hashtable after all?

    Thank you!


    • Edited by Vodalus Thursday, July 18, 2019 8:03 AM spelling
    Thursday, July 18, 2019 7:41 AM

Answers

  • Your convert is creating an array of hash tables and not just a single hash table.

    $ht2 = Get-Content -Path .\file.txt  -Raw | ConvertFrom-StringData


    \_(ツ)_/


    • Edited by jrvModerator Thursday, July 18, 2019 8:11 AM
    • Marked as answer by Vodalus Thursday, July 18, 2019 8:21 AM
    Thursday, July 18, 2019 8:10 AM
    Moderator

All replies

  • You can only Add hash tables - "+=" is only for collections and valuetypes. A hashtable is not a collection or valuetype.

    The  correct syntax is:

    $h1 + $h2


    \_(ツ)_/

    Thursday, July 18, 2019 7:54 AM
    Moderator
  • But it doesn't work :( It gives the same error:

    PS > $ht1 + $ht2
    A hash table can only be added to another hash table.
    At line:1 char:1
    + $ht1 + $ht2
    + ~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : AddHashTableToNonHashTable
    Thursday, July 18, 2019 8:00 AM
  • Your convert is creating an array of hash tables and not just a single hash table.

    $ht2 = Get-Content -Path .\file.txt  -Raw | ConvertFrom-StringData


    \_(ツ)_/


    • Edited by jrvModerator Thursday, July 18, 2019 8:11 AM
    • Marked as answer by Vodalus Thursday, July 18, 2019 8:21 AM
    Thursday, July 18, 2019 8:10 AM
    Moderator
  • This is it, thank you!
    Thursday, July 18, 2019 8:21 AM