Skip to content

Instantly share code, notes, and snippets.

@MVKozlov
Last active Dec 8, 2015
Embed
What would you like to do?
$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
'@
## Sort by length
$list -split "`n" | Sort-Object Length
## Sort by Length without count
$list -split "`n" | Sort-Object { ($_-replace '^\d+\s').Length }
## Custom object
$list -split "`n" | ForEach-Object {
if ($_ -match '^(\d+)\s+(.*)') {
[PSCustomObject]@{
Count = $matches[1]
Item = $matches[2]
}
}
}
## Bird-related items from CustomObjects
$list -split "`n" | ForEach-Object {
if ($_ -match '^(\d+)\s+(.*)') {
[PSCustomObject]@{
Count = $matches[1]
Item = $matches[2]
}
}
} |
Where-Object { $_.Item -match 'Partridge|Dove|Hen|Bird|Geese|Swan' } |
Measure-Object -Sum -Property Count |
Select-Object -ExpandProperty Sum
## Total count
$list -split "`n" | ForEach-Object {
if ($_ -match '^(\d+)\s+(.*)') {
[PSCustomObject]@{
Count = $matches[1]
Item = $matches[2]
}
}
} |
Measure-Object -Sum -Property Count |
Select-Object -ExpandProperty Sum
## Total count bonus
$list -split "`n" | ForEach-Object {
if ($_ -match '^(\d+)\s+(.*)') {
[PSCustomObject]@{
Count = $matches[1]
Item = $matches[2]
}
}
} |
Foreach-Object -Begin {
$total, $sum = 0, 0
} -Process {
$sum = $sum + $_.Count
$total += $sum
} -End {
$total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment