in reply to 99 bottles of beer on the Wall
Not bad for a first program ... here is a less redundant
version in the interest of helping you learn more:
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'.#!/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' ; }
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)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: (jeffa) Re: 99 bottles of beer on the Wall
by mystik (Sexton) on Aug 21, 2002 at 12:33 UTC | |
Re: 99 bottles of beer on the Wall
by Abigail-II (Bishop) on Aug 21, 2002 at 13:11 UTC | |
Re: (jeffa) Re: 99 bottles of beer on the Wall
by ignatz (Vicar) on Aug 21, 2002 at 13:24 UTC | |
by ignatz (Vicar) on Aug 21, 2002 at 13:36 UTC |
In Section
Code Catacombs