Remove a Hash Table from an Array of Hash Tables

Take a look at this post’s title. I got that as a question, from a coworker yesterday. I didn’t know the answer right away, but I took some time last night and a half hour or so today to put it to bed. As of now, I am able to remove a hash table from an array of hash tables. Ugh.

The below array is the one you get when you don’t cast the variable. That’s to say the $x variable doesn’t need [System.Array] in front of it. It effectively does nothing different to that variable. The problem with this type of array is that it’s partially immutable. We can’t remove values from the array.

[System.Array]$x = 'one','two','three'

Therefore, we either need to create a new array to hold the hash tables we still want (as we are removing one of them), or cast our array as [System.Collections.ArrayList]. A variable cast as such allows us to remove values from it.

[System.Collections.ArrayList]$y = 'four','five','six'

Use this next information as a key, or legend, for the further below code. The entire code can be copied into the ISE, or Visual Studio Code, and run.

1. Removes the user-defined variables used in the below code.
2. Creates three hash tables that each include a User key, a Number key, and a Color key.
3. Prompts user to enter 1 or 2 whether they want to use the same array, or a new one.*

* The same array indicates we’re casting our variable as [System.Collections.ArrayList] and using the same variable. The new array indicates we’re casting a new variable as [System.Array], which again is the default and doesn’t actually require a cast.

4. Creates an array of hash tables based on the value 1 or 2.
5. Displays the current array of hash tables (before any changes).
6. Loops through the values in the array and uses the same array (if 1 was chosen), or creates a new array (if 2 was chosen).
7. Displays the updated array of hash tables (removes the hash table that includes the User “bsmith”).

#1 Remove variables (not using function/function scope).
Clear-Host
Remove-Variable -Name Hash1,Hash2,Hash3,Option,HashtableArray,i,HashtableArrayNew -ErrorAction SilentlyContinue 

#2 Create hash tables.
$Hash1 = @{User = 'landrews';Number = 1;Color = 'Red'}
$Hash2 = @{User = 'bsmith';Number = 2; Color = 'Blue'}
$Hash3 = @{User = 'sjackson';Number = 3;Color = 'Yellow'}

#3 Set SameArray vs. NewArray.
Write-Output -InputObject 'This function is hard coded to remove the hash table that include "bsmith" from an array.'
Do {
    $Option = Read-Host -Prompt 'Press 1 to use the same array, or 2 to create a new array'
} Until ($Option -eq 1 -or $Option -eq 2)

#4 Create array of hash tables.
Switch ($Option) {
    '1' {[System.Collections.ArrayList]$HashtableArray = $Hash1,$Hash2,$Hash3; break}
    '2' {[System.Array]$HashtableArray = $Hash1,$Hash2,$Hash3}
}

#5 Display unmodified hash table.
###################################
$HashtableArray
"'''''''^ Array of hash tables bfore ^''''''"
"'''''''v Array of hash tables after v''''''"
###################################

#6 Loop through array of hash tables.
For ($i = 0; $i -le $HashtableArray.Count - 1; $i++) {
    Switch ($Option) {
        '1' {
            If ($HashtableArray[$i].Values -contains 'bsmith') {
                $HashtableArray.Remove($HashtableArray[$i])
            }
        }
        '2' {
            If (-Not($HashtableArray[$i].Values -contains 'bsmith')) {
                [System.Array]$HashtableArrayNew += $HashtableArray[$i]
            }
        }
    }
}

#7 Display updated array.
Switch ($Option) {
    '1' {$HashtableArray}
    '2' {$HashtableArrayNew}
}

The below results show the exact same thing when run either by entering 1 or 2. The difference is the variable that’s displayed. You can see the above variable that’s returned based on 1 or 2. If it’s 1, we display the $HashtableArray variable (the one we created initially). If it’s 2, we display the $HashtableArrayNew variable. That’s the one we create, since we can’t modify the existing $HashtableArray variable when it’s cast as [System.Array].

This function is hard coded to remove the hash table that includes "bsmith" from an array.
Press 1 to use the same array, or 2 to create a new array: 2

Name                           Value
----                           -----
Color                          Red
Number                         1
User                           landrews
Color                          Blue
Number                         2
User                           bsmith
Color                          Yellow
Number                         3
User                           sjackson
'''''''^ Array of hash tables bfore ^''''''
'''''''v Array of hash tables after v''''''
Color                          Red
Number                         1
User                           landrews
Color                          Yellow
Number                         3
User                           sjackson

And with that, I’m done here. Fun stuff. Mostly.

Leave a Reply

Your email address will not be published. Required fields are marked *