Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)

In reply to (jeffa) Re: 99 bottles of beer on the Wall by jeffa
in thread 99 bottles of beer on the Wall by legolas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found