Teaching  ArraysAndHashesLab

This site is powered by the TWiki collaboration platform Powered by PerlBiowiki content is in the public domain.
Comments on this site? ArraysAndHashesLab">Send feedback

Biowiki . Teaching . ArraysAndHashesLab

Biowiki . Teaching . ArraysAndHashesLab

Arrays and Hashes in Perl

By the end of this lab, you should know:

0. Before we get started...

1. Arrays

By now, you already know the basics of writing and executing Perl scripts, and you even wrote one for reverse complementing sequences in a FASTA file.

As you begin to write more complex programs, data structures like arrays and hashes will become increasingly useful. These list structures are great for storing a set of items and providing easy access to them. At a very simplistic level, the major difference between arrays and hashes is just the way you access the elements within them - in an array, you access elements using integer indices and in a hash, you access elements using keys.

The word "key" is ambiguous on purpose, because nearly anything can be a key. What happens on a very low level when you ask for the element with the key X in a hash is to convert X into some numerical value using some built-in hash function and then use that numerical value to access an internal array. Why then, you might ask, wouldn't I write my own array and my own hash function? Well, you could and in some programming languages, you have to if you want to use such a structure. But, the purpose of hashes (aka hash tables) is to provide very quick access to its elements and this all comes down to a good hash function. In computer science classes, you can spend weeks talking about what's a good hash function, so for most of us, using built-in hashes means not having to worry about any of this.

So with that in mind, let's start with arrays. Arrays are useful for storing a list of similar items, e.g. a list of numbers, a list of strings, and even a list of lists. You've already used arrays in the last lab - the @ARGV you used to retrieve command line arguments is an array of strings that Perl creates for you. Perl has a lot of built-in functions for working with arrays and lists in general and we're definitely not going to go through all of them. See the perldoc at perl.org for a complete listing.

2. Hashes

Now that we've played around with arrays, let's move on to hashes. As mentioned before, hashes are pretty similar to arrays, except that instead of using integers to access them, you use keys.

Time to do a Perl exercise (Homework)


Administrative Concerns:

-- AngiChau - 25 Sep 2005

I Attachment History Action Size Date Who Comment
Fastafasta hemoglobin.fasta r1 manage 7.6 K 2007-09-24 - 22:05 JoshKittleson  

----- Revision r141 - 2010-09-29 - 17:49:38 - MohammadAzimi
Biowiki content is in the public domain.
Comments on this site? ArraysAndHashesLab">Send feedback