-
-
Save MVKozlov/8a479d524c7fb5fa767f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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