99 Bottles Of Beer (can't we do better)
```! /usr/bin/perl
# Jim Menard     jimm@{bbn,io}.com     (617) 873-4326    http://www
+.io.com/~jimm/
\$nBottles = \$ARGV[0];
\$nBottles = 100 if \$nBottles eq '' || \$nBottles < 0;

foreach (reverse(1 .. \$nBottles)) {
\$s = (\$_ == 1) ? "" : "s";
\$oneLessS = (\$_ == 2) ? "" : "s";
print "\n\$_ bottle\$s of beer on the wall,\n";
print "\$_ bottle\$s of beer,\n";
print "Take one down, pass it around,\n";
print \$_ - 1, " bottle\$oneLessS of beer on the wall\n";
}
print "\n*burp*\n";

Replies are listed 'Best First'.
Re: 99 Bottles Of Beer (can't we do better)
by mojotoad (Monsignor) on Feb 20, 2002 at 06:05 UTC
Anonsub hell, with proper pronoun management (tm)...
```#!/usr/bin/perl

\$nBottles = abs(shift || 100);

\$p_sub = sub { 'Take ' . (shift == 1 ? 'it' : 'one') .
" down, pass it around,\n" };
\$b_sub = sub { 'bottle' . (shift != 1 ? 's' : '') };
\$c_sub = sub { \$_ = shift||'No'; "\$_ " . &\$b_sub(\$_) . ' of beer' };
\$w_sub = sub { &\$c_sub(shift) . " on the wall"; };
\$sub   = sub { \$_ = shift; print &\$w_sub(\$_), ",\n", &\$c_sub(\$_),
",\n", &\$p_sub(\$_), &\$w_sub(--\$_), ".\n\n" };

map(&\$sub(\$_), reverse(1 .. \$nBottles));
print "burp!\n";
Matt
Re: 99 Bottles Of Beer (can't we do better)
by screamingeagle (Curate) on Feb 20, 2002 at 05:25 UTC
well,if its golf we're playing , then a couple of minor changes saves a few lines :
```\$nBottles = (\$ARGV[0] > 0) ? \$ARGV[0] :100;
foreach (reverse(1 .. \$nBottles)) {
\$s = (\$_ == (1 || 2)) ? "" : "s";
print "\n\$_ bottle\$s of beer on the wall,\n\$_ bottle\$s of beer,\nTake
+one down, pass it around,\n" , (\$_ - 1)," bottle",((\$_ - 1) == 1) ? "
+ " : "s "," of beer on the wall\n";
}
print "\n*burp*\n";

Second Update Reduced by a couple more lines :
```\$nBottles = (\$ARGV[0] > 0) ? \$ARGV[0] :100;
foreach (reverse(1 .. \$nBottles)) {
print "\n\$_ bottle",(\$_ == (1 || 2)) ? "" : "s"," of beer on the wall,
+\n\$_ bottle",(\$_ == (1 || 2)) ? "" : "s"," of beer,\nTake one down, p
+ass it around,\n" , (\$_ - 1)," bottle",((\$_ - 1) == 1) ? " " : "s ","
+ of beer on the wall\n";}print "\n*burp*\n";
Update: fixed a typo (thanks to particle)
Re: 99 Bottles Of Beer (can't we do better)
by particle (Vicar) on Feb 20, 2002 at 09:04 UTC
bad with pronouns, but i'm too tired to fix it up better... (162 - CRLF only to be web-friendly)

```\$a=shift;@b=(" bottles of beer"," on the wall,\$/",
"\$/Take one down, pass it around,");
do{print\$a,@b[0,1],\$a,@b[0,2];print\$/,--\$a,@b[0,1],\$/}
while\$a;print"*burp*";

~Particle

Re: 99 Bottles Of Beer (can't we do better)
by jmcnamara (Monsignor) on Feb 20, 2002 at 08:29 UTC

You aren't the only one who thinks that it could be done better.

Have a look at this thread on clpm.

--
John.

Re: 99 Bottles Of Beer (can't we do better)
by runrig (Abbot) on Feb 20, 2002 at 05:20 UTC
My favorite is still the one in the sample scripts for expect.