| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

10gbPearlTeacher

Page history last edited by Rob Poulter 16 years, 6 months ago

Perl

10 Green Bottles

by Rob Poulter

 

 

# 10 Green Bottles in Perl
my $bottles = 10; # original number of bottles

# for loop to iterate through the bottles in reverse order
for (my $i = $bottles; $i > 0; $i--)
{
  # Default plural for this number of bottles
  my $plural = "s";
  # Change the plural to none if there is only one bottle
  $plural = "" if ($i == 1);
  # The plural of the next number of bottles
  my $next_plural = "s";
  # Change the plural if the current number of bottles is two
  $next_plural = "" if ($i == 2);
  # How many bottles are there next time?
  my $next = $i - 1;
  # Change next to the word NO if there is only one bottle now
  $next = "NO" if ($i == 1);
  print "$i green bottle$plural, sitting on the wall!n";
  print "And if one green bottle should accidentally fall,n";
  print "There'd be $next green bottle$plural, sitting on the wall!nn";

 

Comments (0)

You don't have permission to comment on this page.