Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

99 bottles of beer on the Wall

by legolas (Acolyte)
on Aug 21, 2002 at 02:31 UTC ( #191643=sourcecode: print w/ replies, xml ) Need Help??

Category: Fun Stuff
Author/Contact Info Kevin Cohe
Description: This was my first program. I did it after about an hour or two of reading Learning Perl. I relize there are other programs that do the same thing more efficiently, but this is my first program and I have much to learn.
#!/usr/bin/perl -w

print "Welcome to 99 bottles of beer on the wall\n";

print "How many bottles of beer do you want on the wall\n";

$beer=<STDIN>;

do{

$beer1=$beer-1;

if($beer1>=0){

print "$beer bottles of beer on the wall, $beer bottles of

 beer. Take one down pass it around $beer1 bottles of beer 

on the wall\n";

$beer=$beer-1;

}

}while($beer>0);

Comment on 99 bottles of beer on the Wall
Download Code
(jeffa) Re: 99 bottles of beer on the Wall
by jeffa (Chancellor) on Aug 21, 2002 at 04:35 UTC
    Not bad for a first program ... here is a less redundant version in the interest of helping you learn more:
    #!/usr/bin/perl -w use strict; my $beer = abs shift || 99; print how_many($beer), ",\n", how_many($beer), ".\n", "Take one down, pass it around,\n", how_many(--$beer), ".\n\n" while $beer; sub how_many { my $numb = shift; return $numb . ' bottle' . ($numb == 1 ? '' : 's') . ' of beer on the wall' ; }
    Instead of asking the user for the number of beers *hic*, the code instead gets the number as a command line argument. If no argument is supplied, it instead uses a default value of 99. Also, it counts down until $beer is a false value instead of until $beer is 0 - hence, we have to take the absolute value of that command line argument lest the user give us a negative value and spin us off into an infinite loop. This version also uses a subroutine and a ternary operator (expr ? true : false), which is just a fancy if-else that is used to avoid spouting out '1 bottles'.

    Keep on coding! :)

    UPDATE: oops, you are correct mystik - and yes, my vision was blurry last night! :D (typo corrected)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      I'm pretty sure that you need to change how_many($beer--) to how_many(--$beer). After you take one down, and pass it around, unless your vision is blurrey, there's one fewer beer.

      For the benefit of those that don't know, $var-- returns the value of $var, then decrements it. --$var first decrements, then returns $var

      If the eval engine in my brain is correct, --$var++ will return $var - 1, but the value of $var will still be the same ;)

      And here's a different way:
      #!/usr/bin/perl use strict; use warnings 'all'; use Lingua::EN::Inflect; my $beer = abs (shift || 99); sub UNIVERSAL::TIEHASH {bless [] => shift} sub UNIVERSAL::FETCH {no strict 'refs'; &{"Lingua::EN::Inflect::" . ref $_ [0]} (split /$;/ => $_ [ +1])} tie my %N => 'NUMWORDS'; tie my %B => 'PL'; print <<"--" while $beer; \u$N{$beer} $B{bottle => $beer} of beer on the wall, $N{$beer} $B{bottle => $beer --} of beer. Take one down, pass it around. \u$N{$beer} $B{bottle => $beer} of beer on the wall. -- package NUMWORDS; package PL; __END__
      Getting rid of the warning and making it just a little bit prettier:
      #!/usr/bin/perl -w use strict; use Lingua::EN::Nums2Words; my $beer = shift || 99; $beer = abs($beer); print how_many($beer), ",\n", how_many($beer), ".\n", "Take one down, pass it around,\n", how_many(--$beer), ".\n\n" while $beer; sub how_many { my $numb = &num2word(shift); $numb =~ s/(\w)(.*)/\u$1\L$2/; return $numb . ' bottle' . ($numb eq 'One' ? '' : 's') . ' of beer on the wall' ; }
      Requires Lingua::EN::Nums2Words.
      ()-()
       \"/
        `                                                     
      
        Here's the first joke that I can remember telling as a kid. It still cracks em up today.
        #!/usr/bin/perl -w use strict; use Lingua::EN::Nums2Words; my $beer = shift || 99; $beer = abs($beer); for (-9..$beer) { print how_many($beer), ",\n", how_many($beer), ".\n", "Take one down, pass it around,\n", how_many(--$beer), ".\n\n" } sub how_many { my $numb = &num2word(shift); $numb =~ s/(\w)(.*)/\u$1\L$2/; return $numb . ' bottle' . ($numb eq 'One' ? '' : 's') . ' of beer on the wall' ; }
        ()-()
         \"/
          `                                                     
        

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://191643]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-12-29 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (191 votes), past polls