Old lady swallowed a fly

From Rosetta Code
Revision as of 19:24, 15 August 2011 by MikeMol (talk | contribs) (Should be fairly straightforward, but a draft nonetheless.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Present a program which emits the lyrics to the song I Knew an Old Lady Who Swallowed a Fly, taking advantage of the repetitive structure of the song's lyrics.

Old lady swallowed a fly is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

PHP

<lang php><?php

$swallowed = array(

 array('swallowed' => 'fly.',
       'reason' => "I don't know why she swallowed the fly."),
 array('swallowed' => 'spider,',
       'aside' => "which wiggled and jiggled and tickled inside her.",
       'reason' => "She swallowed the spider to catch the fly"),
 array('swallowed' => 'bird.',
       'aside' => "How absurd! To swallow a bird!",
       'reason' => "She swallowed the bird to catch the spider,"),
 array('swallowed' => 'cat.',
       'aside' => "Imagine that! To swallow a cat!",
       'reason' => "She swallowed the cat to catch the bird."),
 array('swallowed' => 'dog.',
       'aside' => "What a hog! To swallow a dog!",
       'reason' => "She swallowed the dog to catch the cat."),
 array('swallowed' => 'horse',
       'aside' => "She's dead, of course. She swallowed a horse!",
       'reason' => "She swallowed the horse to catch the dog."));

foreach($swallowed as $creature) {

 print "I knew an old lady who swallowed a " . $creature['swallowed'] . "\n";
 if(array_key_exists('aside', $creature))
   print $creature['aside'] . "\n";
 $reversed = array_reverse($swallowed);
 $history = array_slice($reversed, array_search($creature, $reversed));
 foreach($history as $note)
 {
   print $note['reason'] . "\n";
 }
 if($swallowed[count($swallowed) - 1] == $creature)
   print "But she sure died!\n";
 else
   print "Perhaps she'll die." . "\n\n";

}</lang>

Result

I knew an old lady who swallowed a fly.
I don't know why she swallowed the fly.
Perhaps she'll die.

I knew an old lady who swallowed a spider,
which wiggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly
I don't know why she swallowed the fly.
Perhaps she'll die.

I knew an old lady who swallowed a bird.
How absurd! To swallow a bird!
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly
I don't know why she swallowed the fly.
Perhaps she'll die.

I knew an old lady who swallowed a cat.
Imagine that! To swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly
I don't know why she swallowed the fly.
Perhaps she'll die.

I knew an old lady who swallowed a dog.
What a hog! To swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly
I don't know why she swallowed the fly.
Perhaps she'll die.

I knew an old lady who swallowed a horse
She's dead, of course. She swallowed a horse!
She swallowed the horse to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly
I don't know why she swallowed the fly.
But she sure died!