Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PowerShell – Array of HashTables

Creating an Array of HashTables

To create an array of hashtables in PowerShell:

  • Create an array.
  • Create two or more hash tables.
  • Use += operator to add the hash tables (created in the second step) to an array (created in the first step).
$arrayOfHashTables = @()

$hashTable1 = [ordered]@{
                "firstName" = "Mehvish"
                "lastName" = "Ashiq"
                department="Computer Science"
             }
$hashTable2 = [ordered]@{
                "firstName" = "Tahir"
                "Raza" = "Ashiq"
                department="Designs & Arts"
             }

$arrayOfHashTables += $hashTable1
$arrayOfHashTables += $hashTable2

$arrayOfHashTables
Name                  Value
----                  -----
firstName             Mehvish
lastName              Ashiq
department            Computer Science
firstName             Tahir
Raza                  Ashiq
department            Designs & Arts

First, we used an array operator represented with @() to create an array and saved it in the $arrayOfHashTables variable. Next, we created two hash tables stored in $hashTable1 and $hashTable2; each contained key-value pairs wrapped around @{}. Optionally, we can use [ordered] to have key-value pairs in the same order in which they are inserted while creating the hash tables. The array of hash tables will look as follows if we will not use [ordered].

Name               Value
----               -----
department         Computer Science
firstName          Mehvish
lastName           Ashiq
department         Designs & Arts
firstName          Tahir
Raza               Ashiq

Once we are done with creating hash tables, we use the += operator to add $hashTable1 and $hashTable2 in the $arrayOfHashTables. Alternatively, you can also use the + operator to add hash tables to the array of hash tables as follows:

$arrayOfHashTables = @()

$hashTable1 = [ordered]@{
                "firstName" = "Mehvish"
                "lastName" = "Ashiq"
                department="Computer Science"
             }
$hashTable2 = [ordered]@{
                "firstName" = "Tahir"
                "Raza" = "Ashiq"
                department="Designs & Arts"
             }

$arrayOfHashTables = $arrayOfHashTables + $hashTable1
$arrayOfHashTables = $arrayOfHashTables + $hashTable2

We created two hash tables ($hashTable1 and $hashTable2); now, what if we have to add 10 or 15 more? What will happen now? It will result in code redundancy. To avoid that, we will write this in a script named Test.ps1 and pass one or multiple hash tables as parameters. How to do it? Let’s learn that in the following section.

Passing Data from a Parameter to an Array of HashTable

To pass data from a parameter to an array of hash tables:

  • Create a .ps1 file, let’s say Test.ps1.
  • Use the param block to accept an array of hash tables as a parameter.
  • Use the Write-Host cmdlet to display customized messages.
  • Use the foreach loop to iterate over an array of hash tables.
  • Use nested foreach with the GetEnumerator() method to iterate over each hash table entry.
  • For each nested iteration of the above loop, use the Write-Host cmdlet to write the key and its value in key=value format.
param(
    [hashtable[]]$Hashtables
)

Write-Host "Hashtables: @("

foreach($hashtable in $Hashtables){
    Write-Host "  @{"
    foreach($entry in $hashtable.GetEnumerator()){
        Write-Host "    " $entry.Key = $entry.Value
    } 
    Write-Host "  }"
}

Write-Host ")"
.\Test.ps1 -Hashtables @{ firstName = "Aftab"; lastName = "Raza"; department = "Game Development"},@{ firstName = "Azher"; lastName = "Raza"; department = "Mathematics"}
Hashtables: @(
  @{
     department = Game Development
     firstName = Aftab
     lastName = Raza
  }
  @{
     department = Mathematics
     firstName = Azher
     lastName = Raza
  }
)

First, we used the param block with the param keyword followed by opening, and closing brackets (()); this block accepts one or more parameters. We used this to take an array of hash tables as a parameter.

The point is how we can ensure that this script only takes an array of hash tables as a parameter. We can assign a type to the parameter and include a [Parameter()] block. In the following example, we can see a parameter that only accepts an array of hash tables of type hashtable[].

param(
    [Parameter()]
    [hashtable[]]$Hashtables
)

We used Write-Host cmdlet on different places in the above-provided script (Test.ps1) to write customized output/message to the host; here host is our PowerShell console or terminal.

Next, a foreach loop was used to iterate over an array of hash tables ($Hashtables). In each iteration, we used a nested foreach with the GetEnumerator() method to loop over every entry of the hash table and write key=value using Write-Host cmdlet.

What is the role of the GetEnumartor() method? This method was used to get an enumerator (a IEnumerator object) that we used to iterate through the specified collection, which is a hash table in our case.

That’s all about array of HashTables in PowerShell.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

PowerShell – Array of HashTables

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×