1

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!

9
  • 1
    What have you tried so far? Please share your code and what does not work with it. Commented Apr 26, 2022 at 6:40
  • As i am slowly learning Powershell and working with hashtables the first time, i have nothing to really share sadly. I tried different foreach itterations, which i could not get to work, aswell as Group-Object name | sort count -Descending, which does not provide the wanted output. Commented Apr 26, 2022 at 6:59
  • I have added the output of Group-Object name | sort count -Descending above. Commented Apr 26, 2022 at 7:05
  • 2
    So, it's not a Hashtable, but an array of objects. Quite a difference.. Commented Apr 26, 2022 at 7:34
  • 2
    It is probably a lot faster to actually use a hashtable which is based on a binary search: $List |ForEach-Object -Begin { $HashTable = @{} } -Process { $HashTable[$_.Name] += $_.Count } -End { $HashTable } Commented Apr 26, 2022 at 10:51

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.