Perl Home | Notes | Exercises | Resources | perl Thoughts | Jen's Home | Summer Perl Seminar |
Week 4Sample Code
HashesA 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: SubstitutionLast 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: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: 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 2I 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. |