Outdated, will fix later...
100$*99$* bottles of beer
We start by replacing the empty input with 100 1
s (the actual syntax for repetition is count$*character
but if the latter is omitted, it is assumed to be 1
).
.
$.' bottles of beer¶
Now we replace each of those99 1
s with the number of characters after that match (that's $.'
), which goes from 99 down to 0, and appendfollowed by bottles of beer
as well as a linefeed. So now we have:
99 bottles of beer
98 bottles of beer
...
1 bottles of beer
0 bottles of beer
%`$
W1$'W, $_1$'.¶Take one down and pass it around, $_W$'W.¶¶¶
This uses theNow we replace each of those %1
option which means that the stage is applied to each line individually (treating it as a separate string, so there's no way to accesss with the other lines). We matchsubstitution pattern on the end of eachsecond line with. Here $$'
and append the rest ofstands for the versestring after each match, whereand we use $_W
(entire input, i.eas a placeholder for later. in this caseNote that the entireoccurrences of $'
on the first line) so we don't have to repeat are "incremented" by prepending a N bottles of beer1
. We've got most of the work done already, except that the numbers are in unary instead of decimal, and we usewe've got W
as a placeholder for the repeateds instead of on the wall
s and the last verse is off.
1+
$.&
This does the unary-to-decimal conversion by matching each run of 1
s and replacing it with its length.
G-8`3`
This is a "grep" stage. The regex is empty, so it always matches, but the -83
limit means that only lines up to the 8ththird from the end are kept, so the last 7two are discarded. This gets rid of a few extraneous lines like the one with 0
and some empty lines that we don't wanttwo extraneous linefeeds at the end.
T`s``¶1T`s``\b1 .+
This fixes the pluralisation by removing all s
s from the a match of the regex at the end, which matches anything after a 1
(on the same line).
Note that begins with athe last line doesn't have 10
since the unary representation of it is an empty string it wasn't matched by the conversion stage. Instead there will be two spaces in a row.
T.+ 1 \B
Go to the store and buy some more, 99
And finallyThis is the only case where there is no word boundary after a space, so we replace the wrong part ofdetect the very last line like this and replace it with the correct lyrics.