Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Bottle Golf

by inman (Curate)
on Jun 10, 2004 at 21:07 UTC ( #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
Node Status?
node history
Node Type: perlquestion [id://363199]
Approved by grinder
help
Chatterbox?
[ambrus]: The main loop for unixish systems is in https://metacpan. org/source/ KARASIK/Prima-1. 49/unix/apc_app.c. It is a hand-rolled select loop, but also always spins at least once every 0.2 seconds for some reason.
[ambrus]: marto: no, not that way. if you just want to test for an existing id attribute, then write *[@id] , or you can match a regexp to the value of that attribute like *[@id=~/something/ ]
[marto]: ambrus, thanks I ended up going with '//*[starts- with(@id, "thread_")]...
[ambrus]: Sometimes it's easier to just match elements in ways other than xpath, like directly with perl code.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2016-12-08 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (140 votes). Check out past polls.