Skip to content

Instantly share code, notes, and snippets.

@adbertram
Created December 7, 2015 16:19
Show Gist options
  • Save adbertram/3b7622f7ffa47fba17ba to your computer and use it in GitHub Desktop.
Save adbertram/3b7622f7ffa47fba17ba to your computer and use it in GitHub Desktop.
$list = @'
1 Partridge in a pear tree
2 Turtle Doves
3 French Hens
4 Calling Birds
5 Golden Rings
6 Geese a laying
7 Swans a swimming
8 Maids a milking
9 Ladies dancing
10 Lords a leaping
11 Pipers piping
12 Drummers drumming
'@
## Sorting by length
$list -split "`n" | Sort-Object { $_.Length }
## Sorting without number
$list -split "`n" | Sort-Object { ($_ -replace '^\d+ ').Length }
## Custom object
$list -split "`n" | ForEach-Object {
[PSCustomObject]@{ 'Count' = $_.Split(' ')[0]; 'Item' = ($_ -replace '^\d+ ') }
}
## Bird-related items
($list -split "`n" | where { $_ -match 'Partridge|Dove|Hen|Bird|Geese|Swan' } | Select @{ 'Name' = 'Number'; 'Expression' = { $_.Split(' ')[0].Trim() } } | Measure-Object -Sum -Property Number).Sum
## Total count
($list -split "`n" | Select @{ 'Name' = 'Number'; 'Expression' = { $_.Split(' ')[0].Trim() } } | Measure-Object -Sum -Property Number).Sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment