Create HashTable? With 2 Values

Welcome Forums General PowerShell Q&A Create HashTable? With 2 Values

Viewing 3 reply threads
  • Author
    Posts
    • #222081
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Hi,

      I think i need a hash table for this?
      I would like a script to loop through a hash table and for each line, pass the values to my function.

      I have a working solution for 2 columns or a key and a value i believe?:

      $accounts = @
      item1 = 'string_value1'
      item2 = 'string_value2'
      item3 = 'string_value3'
      }
      
      foreach ($line in $accounts.GetEnumerator()){
      Get-MyBalanceFunction -account $($line.Name) -string $($line.Value)
      }

      My issue is trying to add a second value, I would like the list to have 3 columns which i believe would be a key and 2 values?

      Example (which im sure is not how i should do it):

      $accounts = @
      item1 = 'string_value1','int_1'
      item2 = 'string_value2','int_2'
      item3 = 'string_value3','int_3'
      }
      
      foreach ($line in $accounts.GetEnumerator()){
      Get-MyBalanceFunction -account $($line.Name) -string $($line.Value1) -int $($line.Value2)
      }

       

      Any help on achieving this would be much appreciated.

       

      Thanks

      Jamie

      • This topic was modified 1 month, 1 week ago by JDobbsy1987.
    • #228499
      Participant
      Topics: 0
      Replies: 14
      Points: 177
      Helping Hand
      Rank: Participant

      Hello Jamie,

      When you add second value,  your hash table values become arrays and you can access them using index.

      
      $accounts = [ordered]@{
      item1 = 'string_value1','int_1'
      item2 = 'string_value2','int_2'
      item3 = 'string_value3','int_3'
      }
      
      foreach ($line in $accounts.GetEnumerator()){
      Get-MyBalanceFunction -account $($line.Name) -string $($line.Value[0]) -int $($line.Value[1])
      }
      
      

       

       

    • #228505
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      A good old plain CSV file provides the same results and is much less complex I think.

      $accounts = 
      @'
      Name,Value
      John,1
      George,2
      Paul,3
      Ringo,4
      '@ |
          ConvertFrom-Csv 
      
      foreach ($line in $accounts) {
          "Name: '$($line.Name)' - Value: '$($line.Value)'"
      }
    • #228961
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      Helping Hand
      Rank: Community Hero

      Hash tables have their uses, but Powershell most uses PSObject, which from a basic standpoint is an array of hashtables. Take a look at these basic examples:

      #Manual
      $props = @{
          FirstName = 'Alice'
          LastName  = 'Smith'
      }
      
       New-Object -TypeName PSObject -Property $props
      
      #Accelerator
      
       [PSCustomObject]@{
          FirstName = 'Alice'
          LastName  = 'Smith'
       }
      

      When anything is imported, it is typically generating a PSObject, such as Import-CSV. Without seeing the function it’s hard to assist, but the name of params (while I’m sure it’s just an example) is more of a type than a proper param name. If you look at this, you can see how an imported CSV is processed by the function:

      function Get-MyBalanceFunction {
          [CmdletBinding()]
          param (
              [Parameter(
                  Mandatory=$true,
                  ValueFromPipelineByPropertyName=$true
              )]
              [string]$Account,
              [Parameter(
                  Mandatory=$true,
                  ValueFromPipelineByPropertyName=$true
              )]
              [string]$AccountNumber,
              [Parameter(
                  Mandatory=$true,
                  ValueFromPipelineByPropertyName=$true
              )]
              [float]$Amount
          )
          begin {}
          process {
              Write-Verbose ('Processing account {0} ({1}) with amount {2}' -f $Account, $AccountNumber, $Amount)
          }
          end {}
      }
      
      $csv = @"
      Account, AccountNumber, Amount
      Smith Farms,42241344,10023.23
      Johnson Farms,63424242,-23320.44
      AgriFam,23434345,14436.58
      "@ | ConvertFrom-CSV
      
      foreach ($acct in $csv) {
          Get-MyBalanceFunction -Account $acct.Account -AccountNumber $acct.AccountNumber -Amount $acct.Amount -Verbose
      }
      

      But you can even use the pipeline rather than the loop, as simple as this:

      $csv | Get-MyBalanceFunction -Verbose
      

      Both produce the same output:

      VERBOSE: Processing account Smith Farms (42241344) with amount 10023.23
      VERBOSE: Processing account Johnson Farms (63424242) with amount -23320.44
      VERBOSE: Processing account AgriFam (23434345) with amount 14436.58
      

      However, when I see ‘balance’, you can also do something like this:

      PS C:\Users\rasim> $csv | Measure-Object -Property Amount -Sum -Average
      
      Count             : 3
      Average           : 379.79
      Sum               : 1139.37
      Maximum           :
      Minimum           :
      StandardDeviation :
      Property          : Amount
      
Viewing 3 reply threads
  • You must be logged in to reply to this topic.