Perl Home Notes Exercises Resources perl Thoughts Jen's Home Summer Perl Seminar

Week 4

Sample Code

  • adfgx.pl - the adfgx encryption code

    Hashes

    A hash is a data structure. It has keys and values. Each key is mapped to a value. For example, your key could be an email address and it would map to the name of the owner of that address. The name of the owner would be the value.

    To create an empty hash, you start it with a % sign:

    %hash = (); If you want to store information in the hash, you put it in curly braces. Interestingly, you use a $ before the name of the hash, not the % like you use when you create it. In this example, the email address is mapped to a name: $hash{"bob\@example.com"} = "Bob Smith"; To retrieve a value out of the hash, you put the key name in the curly braces. If you want to print the name of this owner, you could do it like this: $email = "bob\@example.com"; print "The address $email belongs to "; print $hash{$email}; You can get a list of keys using the keys special keyword: @listOfKeys = keys %hash; This is most commonly used in a loop. We could loop through each key in the hash and print the value like this: foreach $key (keys %hash) { print "The value is " . $hash{$key} . "\n"; }

    Substitution

    Last week we learned how to create patterns and to see if a string contained a pattern. You can also use patterns to substitute a pattern with something else. The general syntax for this is: $string =~ s/pattern to substitute/thing to substitute it with/; We always start with the variable storing the string and use the =~ operator. Then, we have two patterns. There are three / marks. The pattern containing the thing to replace goes between the first to slashes. The thing to replace it with goes between the second two. We also always start it witih an "s" to indicate we are substituting.

    For example, say we want to substitute all the number 1 with the word "one" in the string "That's 1 small step for a man, 1 giant leap for man kind." We can start by doing this:

    $string = "That's 1 small step for a man, 1 giant leap for man kind." $string =~ s/1/one/; If you try that code, you will see it replaces the first 1, but not the second. This is because the substitution only replaces the first instance. If we want to replace them all, we add a "g" to the end to indicate global replacement. $string = "That's 1 small step for a man, 1 giant leap for man kind." $string =~ s/1/one/g; There is another letter we can put with the g - an i. The i indicates that we should ignore the case, making our substitution case insensitive. You can use just the g, just the i, or both together.

    We use both substitution and hashes in the example code which implements the ADFGX cipher. You can find more details about this in the exercises for this week. [an error occurred while processing this directive]

    Exercise 2

    I am providing you with a list of tags that users applied to a series of images in the steve project and a list of titles of wikipedia articles. Each line in the steve data file has two identifiers, the tag, and a third identifier separated by tabs.

    Your job is to find multi-word tags (i.e. tags that are more than one word) and, if a tag matches a wikipedia article title, print either the tag or the entire line (your choice) from the file that has that tag.

    You must use hashes and loop through the contents of each file only once. You man NOT put the values in a data structure and loop many times - you can only loop once.

    You must work with local versions of these files - do NOT access them using LWP even while you are working on the assignment. Keep the names of the files identical to how they are linked here. Do not upload copies of these files when you turn in your assignment.