Home > database >  Powershell array of objects add together objects with same value
Powershell array of objects add together objects with same value

Time:04-27

I want to add together counts with equal names. The array looks something like this:

Name  Count
----  -----
6027     12
4999      6
3018      5
1008      3
1006     19
6027     12
4065     10
3018      9
4999      7
489       4
1008      3
5016     19

I would like to add up all counts with equal names. Wanted result would look like this:

Name  Count
----  -----
6027     24
4999      6
3018     14
1008      6
1006     19
4065     10
4999      7
489       4
5016     19

My best take is to Group the Object by name thus far, which does not produce the wanted output. (Numbers are a bit different than in the above example)

$list | Group-Object name | sort count -Descending
$list

Output:

Count Name                      Group                                                                                                                                                             
----- ----                      -----                                                                                                                                                             
    8 3018                      {@{Name=3018; Count=5}, @{Name=3018; Count=9}, @{Name=3018; Count=9}, @{Name=3018; Count=9}...}                                                                   
    7 6027                      {@{Name=6027; Count=12}, @{Name=6027; Count=12}, @{Name=6027; Count=12}, @{Name=6027; Count=12}...}                                                               
    7 4999                      {@{Name=4999; Count=6}, @{Name=4999; Count=7}, @{Name=4999; Count=16}, @{Name=4999; Count=12}...}                                                                 
    7 1008                      {@{Name=1008; Count=3}, @{Name=1008; Count=3}, @{Name=1008; Count=3}, @{Name=1008; Count=3}...}                                                                   
    4 1006                      {@{Name=1006; Count=19}, @{Name=1006; Count=12}, @{Name=1006; Count=6}, @{Name=1006; Count=30}}                                                                   
    3 4065                      {@{Name=4065; Count=10}, @{Name=4065; Count=35}, @{Name=4065; Count=30}}                                                                                          
    3 5016                      {@{Name=5016; Count=19}, @{Name=5016; Count=18}, @{Name=5016; Count=20}}

Thanks four your help!

CodePudding user response:

iRon's helpful comment proposes a nice and efficient solution to the problem using a hash table where the Keys are the unique values of the Name property and the Values are the sum of the Count property. We can use the generated hash table to update the object you already have.

Below example assumes $obj has the array of objects you have displayed in your question.

$map = @{}
foreach($i in $obj) {
    $map[$i.Name]  = [int] $i.Count
}

foreach($i in $obj) {
    $i.Count = $map[$i.Name]
}

$obj # => Should be now updated
  • Related