www.perl.com
Perl Programming
Hashes
  • Easy to iterate
    • keys returns a list of the keys
    • values returns a list of the values
    • each returns key/value pairs in random order
    • while loop can iterate over entire hash

        my @animals = keys %sounds;
        my @noises = values %sounds;

        while (my ($animal, $noise) = each %sounds) {
            print "Old MacDonald had a $animal.";
            print "  With a $noise! $noise! here...\n";
        }
      

        Output:
        Old MacDonald had a sheep.  With a baa! baa! here...
        Old MacDonald had a cow.  With a moooo! moooo! here...
        Old MacDonald had a hen.  With a cluck! cluck! here...
        Old MacDonald had a pig.  With a oink! oink! here...
        Old MacDonald had a duck.  With a quack! quack! here...
        Old MacDonald had a horse.  With a whinny! whinny! here...
      
Previous http://stuff.mit.edu/iap/perl/