# PowerShell/Arrays and Hash Tables

This lesson introduces PowerShell arrays and hash tables.

## Objectives and Skills

After completing this lesson, you will be able to:
* Describe basic array and hash table concepts
* Create PowerShell scripts that use arrays.
* Create PowerShell scripts that use hash tables.

## Readings

* Wikipedia: Array data type
* Wikipedia: Associative array
* BonusBits: Mastering PowerShell Chapter 4 - Arrays and Hashtables

## Multimedia

* YouTube: Arrays

## Examples
#### Removing Elements from an Array

To remove the elements which are not required in an array, use the below command. This stores only the sub-arrays mentioned here.

#### Adding Items to a Hash Table

Either:
or:

#### Format-Table

The Format-Table cmdlet formats hash table output as a table.

#### Format-List

The Format-List cmdlet formats hash table output as a list of separate key-value pairs.

## Activities

* Review Microsoft TechNet: about_Arrays. Create a script that initializes an array with the names of all of the members of your family. Use Sort-Object to sort and display the names.
* Review Microsoft TechNet: about_Arrays. Create a script that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a for or while loop to store each score in an array. Finally, use a for or foreach loop to calculate and display the average for the entered scores.
* Review Microsoft TechNet: Working with Hash Tables Create a script that initializes a hash table with the names of all of the members of your family. Use Sort-Object and Format-Table to sort and display the names.
* As above, create a script that uses a hash table to hold the names of all of the members of your family and use Sort-Object to sort and display the names. This time, start by initializing an empty hash table, and add each name to the hash table as it is entered by the user. Use Sort-Object and Format-List to sort and display the names.
* Review Microsoft TechNet: about_Hash_Tables. Create a script that asks the user to enter their name, address, city, state, and postal code. Store each entry as an item in a hash table. Display the name and address information as you would for a printed mailing address.

## Lesson Summary

* An array is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices (identifying keys) that can be computed at run time by the program.
* Arrays are distinguished from lists in that arrays allow random access, while lists only allow sequential access. This feature allows a single iterative statement to process arbitrarily many elements of an array variable.
* An index is a value, typically a numeric integer, used to identify and reference an array element.
* Array indexes start at either zero or one, depending on the programming language, and correspondingly end at either the number of elements minus one [0..n-1] or at the number of elements [1..n]. PowerShell arrays are zero-based.
* The number of indices needed to specify an element is called the dimension, dimensionality, or rank of the array type.
* Dynamic arrays are resizable and may be expanded at any time after creation, without changing the values of the current elements.
* Index checking means that, in all expressions indexing an array, the index value is checked against the bounds of the array (which were established when the array was defined), and if the index is out-of-bounds, further execution is suspended via some sort of error.
* A hash table is a data structure used to implement an associative array, a structure that can map keys to values.
* A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.
* An associative array is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.
* PowerShell arrays are initialized using @(value, value) syntax.
* PowerShell array elements are accessed using $array[element] syntax.
* An empty PowerShell array is created using @.
* PowerShell array elements are added using $array += element syntax.
* PowerShell array elements are removed using $array = $array[sub..set] + $array[sub..set] syntax.
* PowerShell multi-dimensional array elements are accessed using $array[element][element] syntax.
* PowerShell hash tables are initialized using @{key = value; key = value} syntax.
* Hash table values are accessed using $table.key syntax.
* An empty PowerShell hash table is created using @.
* PowerShell hash table values are added using $table.Add(key, value) syntax or $table.key = value syntax.
* PowerShell hash table values are removed using $table.Remove(key) syntax.
* The Format-Table cmdlet formats hash table output as a table.
* The Format-List cmdlet formats hash table output as a list of separate key-value pairs.

## Key Terms
### array

    A data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices (identifying keys) that can be computed at run time by the program.

### associative array

    An abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.

### hash table

    A data structure used to implement an associative array, a structure that can map keys to values.

### index

    A value, typically a numeric integer, used to identify and reference an array element.

### key

    A data element which allows one to find an associated data value or values by using a database index, hash table or memory location.

## Assessments

* Flashcards: Quizlet: Windows PowerShell - Arrays and Hash Tables
* Quiz: Quizlet: Windows PowerShell - Arrays and Hash Tables

## See Also

* Technical Sand: Hashing and Consistent hashing