Skip to content

Instantly share code, notes, and snippets.

@MVKozlov
Last active December 8, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MVKozlov/8a479d524c7fb5fa767f to your computer and use it in GitHub Desktop.
Save MVKozlov/8a479d524c7fb5fa767f 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
'@
## 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