Some thoughts on hash tables

I need to test the existence of an element in a hash table.

First create a hash table

$myht = DATA {
ConvertFrom-StringData -StringData @’
1 = aaaaaaa
2 = bbbbbbb
3 = ccccccc
4 = ddddddd
5 = eeeeeee


I tend to create them using ConvertFrom-StringData as it simplifies typing, I have a template for this and its easy to see what entries I’m making

lets see what happens

“1 = $($myht[‘1’])”
“6 = $($myht[‘6’])”


1 = aaaaaaa
6 =


The first test works as we would expect but interestingly we don’t get an error message when we try to access an element that doesn’t exist

So we can build on that

“1”,”6″ | foreach {
if ($($myht[$_])){
  “$_ = $($myht[$_])”
else {
   “$_ not found”


a simple logic test shows if the element exists or not and we can branch from there

Life gets a bit more interesting when we are dealing with boolean and NULL values.  In this case I built hash table with three values as shown, true, false and null

$myht2 = @{“1″=$true; “2”=$false; “3”=$null}


if we run the same if statement

“1”,”2″, “3”, “4” | foreach {
if ($myht2[$_]){
  “$_ = $($myht2[$_])”
else {
   “$_ not found”

we get  this

1 = True
2 not found
3 not found
4 not found

which isn’t quite true

So lets try a switch statement

“1”,”2″, “3”, “4” | foreach {
  $x = $_
  switch ($myht2[$_]){
    $true   {“$x = $($myht2[$x])”}
    $false  {“$x is false”}
    $null   {“$x is null”}
    default {“$x not found”}

1 = True
2 is false
3 is null
4 is null


Answers for 1,2 & 3 are OK but we can’t differentiate between an element that is null or a non-existent element.

The answer is that we need to think carefully about the data in our hash tables and how we test for existance

This entry was posted in Powershell Basics. Bookmark the permalink.

3 Responses to Some thoughts on hash tables

  1. Mark Leighton says:

    The value of a hashtable element has only a loose connection of the element’s existence – as you have demonstrated, its a poor test though commonly done. The correct approach is to use the rich object methods provided to test for what you want. Does the element exist? That is, is there an element labelled with the specified key:

    ‘1’,’2′,’3′,’4′,’5′,’6′ | foreach {
    if ( $myht.ContainsKey($_) ){
    “$_ = $($myht[$_])”
    } else {
    “$_ not found”

  2. scriptengine says:

    Richard I’m confused.
    In your first example why do you not just do:
    that way you always know if a hash element exists and you are no relying on PS side effects.

    Have I totally missed the point?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s