I’ll Tell You What…

March 17, 2008

BeerSong.pl

Filed under: Programming, Perl — Larry @ 2:27 am
This has a much better output. It actually types out the word for the number instead of just the number itself.

Two bottles of beer on the wall.
Two bottles of beer.
Take one down.
Pass it around.
One bottle of beer on the wall.

One bottle of beer on the wall.
One bottle of beer.
Take it down.
Pass it around.
No more bottles of beer on the wall.

#!/usr/bin/perl -w

use strict;
use Lingua::EN::Inflect qw( NUMWORDS );

my ($beerNumber, $beerNum, @num_word);
my $word = "bottles";

$beerNumber = 99;

while ( $beerNumber > 0 ) {
	$beerNum = wordize($beerNumber);
	
	print "$beerNum " . $word . " of beer on the wall.\n";
	print "$beerNum " . $word . " of beer.\n";
	
	if ($beerNumber == 1) {
		print "Take it down.\n";
		$word = "bottle";
	} else {
		print "Take one down.\n";
	}
	
	print "Pass it around.\n";
	
	$beerNumber--;

	if ($beerNumber == 1) {
		$word = "bottle";
	}
	
	$beerNum = wordize($beerNumber);
	
	if ($beerNumber > 0) {
		print "$beerNum " . $word . " of beer on the wall.\n\n";
	} else {		
		print "No more bottles of beer on the wall.\n";
	}
}

sub wordize {
	my (@beerNumWord, $beerNumReturned);
	
	@beerNumWord = NUMWORDS ( shift @_ );
	$beerNumReturned = ucfirst(shift @beerNumWord); 
	return $beerNumReturned;
}

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

24 queries. 0.465 seconds. Powered by WordPress