locked
How to clear a hashtable? RRS feed

  • Question

  • I have this code that creates a hashtable from a sharepoint-list:

    $hashTable = @()
    $items = Get-PnpListItem -List "MyList"
    {
    $HashTable += New-object psobject -Property @{ 
    'Datum' = $item.FieldValues.Datum.ToLocalTime().ToString("dd.MM.yyyy")
    '00:00 - 07:59' = $item.FieldValues._x0030_0_x003a_00_x0020__x002d__;
    '08:00 - 16:59' = $item.FieldValues._x0030_8_x003a_00_x0020__x002d__;
    '17:00 - 23:59' = $item.FieldValues._x0031_7_x003a_00_x0020__x002d__;  
    'Opmerkingen' = $item.FieldValues.Opmerkingen; 
      } 
    } 
    $hashTable | Select Datum,'00:00 - 07:59','08:00 - 16:59','17:00 - 23:59',Opmerkingen | epcsv $DesktopPath + "MyList" -NoT

    I want to reuse this code for multiple lists which has the same columns. Instead of creating a hashtable1, hasthable2. But how can I clear the hashtable. And to I need to clear the other variables in the hashtable too?

    Greetings, P

    Saturday, January 12, 2019 6:34 PM

Answers

  • I would re-initialize the hash table with:

    $Table = @{}

    Edit: A quick example.

    $HT = @{} $HT.Add("a",$True) $HT.Add("b",$True) $HT.Add("c",$True) $HT.Add("d",$True) $HT.Add("e",$True) $HT.Count $HT "---" $HT = @{} $HT.Count

    $HT



    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    • Edited by Richard MuellerMVP Saturday, January 12, 2019 6:51 PM
    • Marked as answer by PeetK Saturday, January 12, 2019 6:52 PM
    Saturday, January 12, 2019 6:42 PM

All replies

  • I would re-initialize the hash table with:

    $Table = @{}

    Edit: A quick example.

    $HT = @{} $HT.Add("a",$True) $HT.Add("b",$True) $HT.Add("c",$True) $HT.Add("d",$True) $HT.Add("e",$True) $HT.Count $HT "---" $HT = @{} $HT.Count

    $HT



    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    • Edited by Richard MuellerMVP Saturday, January 12, 2019 6:51 PM
    • Marked as answer by PeetK Saturday, January 12, 2019 6:52 PM
    Saturday, January 12, 2019 6:42 PM
  • Do I need  to clear the variables that I use in the hashtable too ($item, $items) ?

    Saturday, January 12, 2019 6:47 PM
  • No need. Per the example I just added to my reply, the hash table is empty.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Saturday, January 12, 2019 6:50 PM
  • Thnx
    Saturday, January 12, 2019 6:52 PM
  • You are welcome!

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Saturday, January 12, 2019 7:00 PM
  • $hashTable, in the OP's question, is an array, not a hash. So, $hashTable=@() [using parentheses] instead of $hashTable=@{} [using braces].

    Either one will set the $hashTable to an object with no contents, but still . . .

    $h=@{}
    $h += New-object psobject -Property @{ 'Datum' = 'stuff' }
    . . . results in an error: "A hash table can only be added to another hash table."


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Saturday, January 12, 2019 7:31 PM
  • This is the best, easiest and fastest way to do this if you want a Csv:

    Get-PnpListItem -List MyList |
        ForEach-Object{
            [pscustomobject]@{
                'Datum' = $item.FieldValues.Datum.ToLocalTime().ToString('dd.MM.yyyy')
                '00:00 - 07:59' = $item.FieldValues._x0030_0_x003a_00_x0020__x002d__;
                '08:00 - 16:59' = $item.FieldValues._x0030_8_x003a_00_x0020__x002d__;
                '17:00 - 23:59' = $item.FieldValues._x0031_7_x003a_00_x0020__x002d__;
                'Opmerkingen' = $item.FieldValues.Opmerkingen;
            }
        } |
        Export-Csv $DesktopPath + "MyList" -NoTypeInformation


    \_(ツ)_/

    • Proposed as answer by jrv Saturday, January 12, 2019 8:06 PM
    Saturday, January 12, 2019 8:05 PM
  • I missed entirely that the code used an array. For hash tables I use the .Add method to add (key, value) pairs. I also generally use the .ContainsKey method to ensure uniqueness of keys.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Saturday, January 12, 2019 8:10 PM
  • I missed entirely that the code used an array. For hash tables I use the .Add method to add (key, value) pairs. I also generally use the .ContainsKey method to ensure uniqueness of keys.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Yes but the OP's example can't create dupes and is really just trying to make an exportable object.  A customobject based on a hash is the cleanest and easiest way to do this.

    We can also just use a calculated select property which is fundamentally the same thing.

    I agree. If you are arbitrarily creating a hash from unknown keys then the "Add" and "Contains" method is best.


    \_(ツ)_/

    Saturday, January 12, 2019 9:00 PM