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

  • Buried in cloud files? We can help with Spring cleaning!

    Whether you use Dropbox, Drive, G-Suite, OneDrive, Gmail, Slack, Notion, or all of the above, Dokkio will organize your files for you. Try Dokkio (from the makers of PBworks) for free today.

  • Dokkio (from the makers of PBworks) was #2 on Product Hunt! Check out what people are saying by clicking here.

View
 

10gbPearlTeacher

Page history last edited by Rob Poulter 14 years, 12 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.