![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | sorting on hashtable values Powershellers, I have a hashtable '$Store' consisting of keys paired to multiple arrays Name Value ---- ---- Buns { 3 2, 0 0, 0 0, 2 1} Loaves {4 2, 5 2, 0 0, 0 0} and the list continues. what I'd like to do is sort the keys based on the sum of position zero within each of the arrays For example, I sum all vaules in position zero thus: @($i=0; $Store.Keys | %{$Store.$_[0..4]} | %{$i+=$_[0]}; $i) I can sort the values themselves thus $store.keys | %{$store.$_[0][0]} | sort but I need to display the key as well as the vaule and that position must be based on the cumulative sum of position zero for each of the 4 arrays per item.... any ideas? cheers n |
My System Specs![]() |
| | #2 (permalink) |
| | Re: sorting on hashtable values Instead of evaluating the values and sending the results to sort, just pass sort a script block, like so: $Store.Keys | sort { ($Store[$_][0] | measure-object -sum).Sum } Now what's coming out at the end is a stream of keys instead of values, so you can access back into the hashtable and grab the values back out and display them however you like. Hope this helps. -- Ryan Milligan "Neil Chambers" <nospamplease@nospam.com> wrote in message news:ECA2DA43-B943-438A-A06F-029534BE7E62@microsoft.com... > Powershellers, > > I have a hashtable '$Store' consisting of keys paired to multiple arrays > > Name Value > ---- ---- > Buns { 3 2, 0 0, 0 0, 2 1} > Loaves {4 2, 5 2, 0 0, 0 0} > > and the list continues. > > what I'd like to do is sort the keys based on the sum of position zero > within each of the arrays > > For example, I sum all vaules in position zero thus: > > @($i=0; $Store.Keys | %{$Store.$_[0..4]} | %{$i+=$_[0]}; $i) > > I can sort the values themselves thus > > $store.keys | %{$store.$_[0][0]} | sort > > but I need to display the key as well as the vaule and that position must > be based on the cumulative sum of position zero for each of the 4 arrays > per item.... > > any ideas? > > cheers > n |
My System Specs![]() |
| | #3 (permalink) |
| | Re: sorting on hashtable values On Apr 7, 9:16 pm, "Neil Chambers" <nospample...@nospam.com> wrote: > Powershellers, > > I have a hashtable '$Store' consisting of keys paired to multiple arrays > > Name Value > ---- ---- > Buns { 3 2, 0 0, 0 0, 2 1} > Loaves {4 2, 5 2, 0 0, 0 0} > > and the list continues. > > what I'd like to do is sort the keys based on the sum of position zero > within each of the arrays > > For example, I sum all vaules in position zero thus: > > @($i=0; $Store.Keys | %{$Store.$_[0..4]} | %{$i+=$_[0]}; $i) > > I can sort the values themselves thus > > $store.keys | %{$store.$_[0][0]} | sort > > but I need to display the key as well as the vaule and that position must be > based on the cumulative sum of position zero for each of the 4 arrays per > item.... > > any ideas? > > cheers > n Here's a nuts and bolts approach, though Ryan's already come up with a more elegant solution! I was thinking along the lines of creating a second hashtable to store the order, like so: # Define two two-dimensional arrays $bunsArray = New-Object 'object[,]' 2,4 $loavesArray = New-Object 'object[,]' 2,4 # Populate the arrays $bunsArray[0,0]=3 $bunsArray[1,0]=2 $bunsArray[0,1]=0 $bunsArray[1,1]=0 $bunsArray[0,2]=0 $bunsArray[1,2]=0 $bunsArray[0,3]=2 $bunsArray[1,3]=1 $loavesArray[0,0]=4 $loavesArray[1,0]=2 $loavesArray[0,1]=5 $loavesArray[1,1]=2 $loavesArray[0,2]=0 $loavesArray[1,2]=0 $loavesArray[0,3]=0 $loavesArray[1,3]=0 # Create a hashtable and populate it with the two arrays $store = @{} $store.add('Buns', $bunsArray) $store.add('Loaves', $loavesArray) # Create a hashtable keyed on the sum of the first elements of each array, mapped onto the store key $order=@{} #Iterate through the arrays in the store, sum up the first elements, and populate the order hashtable $store.keys |% { $total=$dim0=$dim1=0; while($dim1 -lt $store.$_.GetLength(1)) { $total += $store.$_[($dim0,$dim1)]; $dim1++ } $order.add($total,$_) } # display each item from the store along with its first element count, followed by all element values, all # ordered on the first element count (ascending) $order.keys | sort |% { [string]$order.$_ + ", count: $_, value: " + [string]$store. ($order.$_)[(0,0),(1,0),(0,1),(1,1),(0,2),(1,2),(0,3),(1,3)] } Regards, Duncan. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: sorting on hashtable values aha! hello 'Measure-Object'. Where have you been all my life ;-) Thanks to Ryan, for introducing me to this lovely cmdlet. And Thank you Duncan, for showing me behind the curtain! : n) |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| comparing hashtable values | PowerShell | |||
| Export-Csv multiple hashtable values | PowerShell | |||
| How to sort a hashtable? | PowerShell | |||
| Assigning new values to a hashtable | PowerShell | |||
| Sorting a hashtable | PowerShell | |||