Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

3. The Dime Tour > Hash Tables

Hash Tables

A hash table or hash map is a data structure that lets you map keys (e.g., a person’s name) to their associated values (e.g., the person’s telephone number). A hash table implements an associative array.

Creating an Empty Hash Table

The @{} creates an empty hash table, similar to the @() used to create the empty array.

$h = @{}
$h.Count
0
$h.GetType()
IsPublic IsSerial Name      BaseType
-------- -------- ----      --------
True     True     Hashtable System.Object

Adding a Hash Table Item

Once we have an empty hash table, we can map keys and values to it. With PowerShell, we can use either the traditional approach or dot notation:

$h = @{}
$h["Item0"] = 0 # More ceremony
$h.Item1 = 1 # Notice, dot notation
$h.Item2 = 2
$h # Prints the Hash table
Name  Value
----  -----
Item1 1
Item0 0
Item2 2

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial