Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum multiple items in an object in PowerShell?

Tags:

powershell

I have:

$report.gettype().name Object[]  echo $report  Item                       Average --                         ------- orange                     0.294117647058824 orange                    -0.901960784313726 orange                    -0.901960784313726 grape                      9.91335740072202 grape                      0 pear                       3.48736462093863 pear                      -0.0324909747292419 pear                      -0.0324909747292419 apple                     12.1261261261261 apple                     -0.0045045045045045 

I want to create a variable, $total, (such as a hash table) which contains the sum of the 'Average' column for each item, for example,

echo $total  orange  -1.5097 grape    9.913 pear     3.423 apple   12.116 

Right now I'm thinking of looping through the $report, but it's hell ugly, and I am looking for something more elegant than the following starting point (incomplete):

$tmpPrev = "" foreach($r in $report){     $tmp = $r.item     $subtotal = 0     if($tmp <> $tmpPrev){         $subtotal += $r.average     } 

How could I do this?

like image 790
ted Avatar asked May 14 '11 05:05

ted


People also ask

How do I count objects in PowerShell?

You can use Measure-Object to count objects or count objects with a specified Property. You can also use Measure-Object to calculate the Minimum, Maximum, Sum, StandardDeviation and Average of numeric values. For String objects, you can also use Measure-Object to count the number of lines, words, and characters.

What does $_ do in PowerShell?

The $_ is a variable or also referred to as an operator in PowerShell that is used to retrieve only specific values from the field. It is piped with various cmdlets and used in the “Where” , “Where-Object“, and “ForEach-Object” clauses of the PowerShell.

What is @() in PowerShell?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.

How do you add numbers in PowerShell?

Adding is as simple as just typing out the numbers that you wish to add together. You can add as many numbers together that you want as long as you end the command with a number: As you can see, PowerShell knows that we are looking to perform an arithmetic operation and proceeds to display the solution to the problem.


1 Answers

Cmdlets Group-Object and Measure-Object help to solve the task in a PowerShell-ish way:

Code:

# Demo input $report = @(     New-Object psobject -Property @{ Item = 'orange'; Average = 1 }     New-Object psobject -Property @{ Item = 'orange'; Average = 2 }     New-Object psobject -Property @{ Item = 'grape'; Average = 3 }     New-Object psobject -Property @{ Item = 'grape'; Average = 4 } )  # Process: group by 'Item' then sum 'Average' for each group # and create output objects on the fly $report | Group-Object Item | %{     New-Object psobject -Property @{         Item = $_.Name         Sum = ($_.Group | Measure-Object Average -Sum).Sum     } } 

Output:

Sum Item --- ----   3 orange   7 grape 
like image 108
Roman Kuzmin Avatar answered Oct 20 '22 02:10

Roman Kuzmin