Monthly Archive

Categories

Modifying hashtables

Saw a question about modifying hashtables. The suggested code seemed like overkill.

The starting point is this hashtable:

$record = @{Name='Joe'; Date='2019-02-01'; Status='Pending'}

 

The updated data is:
$update = @{Name='Joe'; Date='2019-04-01'; Status='Hired'; City='York'; Country='England'}

 

You need to keep the Name and Date from $record. Modify the Status to be that of $update and add the City and County information to record.

Changing status is a simple assignment:

$record.Status = $update.Status

 

Adding the City and County information is done by adding them to the original hashtable.

$record += @{City=$update.City; Country=$update.Country}

 

You can check the results by displaying the hashtable.

 

Technically, you’re creating a new hashtable, adding the information from $record and then the City and Country information but as it stays in the original variable its always referred to as modifying.

Comments are closed.