Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Bottle Golf

by inman (Curate)
on Jun 10, 2004 at 21:07 UTC ( [id://363199]=perlquestion: print w/replies, xml ) Need Help??

inman has asked for the wisdom of the Perl Monks concerning the following question:

Probably done before. Generate the rhyme '10 green bottles...'
#! /usr/bin/perl -w foreach (reverse 1..10){ sub wall {$a=$_-1?'s':'';"$_ green bottle$a, standing on a wall\n"} print wall($_)x2, "and if 1 green bottle should accidentally fall\nthere'll be ", wall(--$_),"\n"}
Any other methods?

Replies are listed 'Best First'.
Re: Bottle Golf
by Enlil (Parson) on Jun 10, 2004 at 21:44 UTC
      IIRC, the shortest known Perl solution to this problem was uncorked by Ton Hospel and Mtv Europe and, oddly, is documented in the 99 Bottles of Beer section of Acme::EyeDrops documentation.
Re: Bottle Golf
by shemp (Deacon) on Jun 10, 2004 at 21:32 UTC
    whoops, i guess my first attempt was a little off. Also, bottle should be pluralized except when there is 1 bottle left. Even zero bottles is plural.

    played a little more, here something better, without a sub
    print join "\n", map { "and if 1 green bottle should accidentally fall +\n" ."there'll be " . ($_-1) . " green bottle" .($_!=2?'s':'') . ", standing on a wall\n" } (reverse 1..10);

    (hopefully correct)
      This may be a bit off topic, but could people let me know why i got voted down for this post?
      Im thoroughly perplexed!
        There are any number of reasons that people downvote, ranging from thinking your post was overrated to XP whoring to retaliation for somebody doing it to them. If you don't see a problem with your post and they can't be bothered to point it out, don't worry about it.

        We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: Bottle Golf
by shemp (Deacon) on Jun 10, 2004 at 21:24 UTC
    #!/usr/bin/perl -w print join "\n", map { "and if 1 green bottle should accidentally fall +\n" ."there'll be " . ($_-1) . " green bottle" .plural($_) . ", standing on a wall\n" } (reverse 1..10); sub plural {return $_-1?'s':'';}
    whitespace added for clarity

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://363199]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found