Skip to content

Instantly share code, notes, and snippets.

@ThePSAdmin
Last active June 4, 2020 14:24
Show Gist options
  • Save ThePSAdmin/b5fbe87503dda843f5855b0f5de5c62b to your computer and use it in GitHub Desktop.
Save ThePSAdmin/b5fbe87503dda843f5855b0f5de5c62b to your computer and use it in GitHub Desktop.
((((99..1)+'no more')|%{}{$y,$a=" on the wall"," bottle$(,'s'[$_-eq1]) of beer"
"Take one down and pass it around, $_$a$y.`n"
"$_$a$y, $_$a."-replace'^n',"N"}{"Go to the store and buy some more, 99$a$y."})[1..200])|write-host
@PalmEmanuel
Copy link

nal a Write-Host
$b=' bottles of beer'
$w=' on the wall'
$n='o more'
99..1|%{
    ("$_$b$w, $_$b.`nTake one down and pass it around, $($_-1)$b$w.`n"-replace' 0'," n$n")-replace'(\b1 \w+)s','$1'|a
}
a "N$n$b$w, n$n$b.`nGo to the store and buy some more, 99$b$w."

@ThePSAdmin
Copy link
Author

Nice! You can shorten the code somewhat by using -Begin -Process -End params of foreach-object positionally and assigning vars on the same line:

$b,$w,$n=' bottles of beer',' on the wall','o more'
99..1|%{}{("$_$b$w, $_$b.`nTake one down and pass it around, $($_-1)$b$w.`n"-replace' 0'," n$n")-replace'(\b1 \w+)s','$1'}{"N$n$b$w, n$n$b.`nGo to the store and buy some more, 99$b$w."}|write-host

@PalmEmanuel
Copy link

That's clever! You took mine down below 250 as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment