next up previous contents
Next: Writing functions Up: Programming in Perl Previous: Matching and regular expressions   Contents

Associative arrays

In addition to regular arrays indexed by position, Perl has another collective type called an associative array, or a hash. A hash is a set of pairs of the form (key,value). The keys of a hash are strings. Collectively, the name of a hash starts with a %, unlike $ for scalars and @ for arrays. To access an element of a hash we use braces rather than square brackets. The function keys extracts the keys from a hash table. Here is an example.

   $phone{"CMI"} = 28157814;
   $phone{"IMSc"} = 22541856;
   $phone{"Home"} = 24404396;
   foreach $k (keys %phone){
     print $k, ":", $phone{$k}, "\n";
   }

The output generated by this will be something like

   CMI : 28157814
   Home : 24404396
   IMSc : 22541856

Observe that the sequence in which keys listed out the keys of %phone is not the same as the sequence in which the keys were added to the hash. This usually happens in a Perl hash, due to internal optimization.

Here is a typical use of a hash. We read data about students' marks in a course from a file in which each line is of the form rollnumber marks. The file records marks from multiple evaluations (tests, assignments, ...) so each rollnumber appears more than once, in general. We split each line of input using the builtin function split that splits a string along whitespace boundaries and returns a list whose elements are the constituents of the string after splitting. We accumulate the marks for each student in a hash and print out a summary at the end.

  while ($line = <MARKS>){
    ($rollno, $marks) = split $line;  # split $line on whitespace boundary
    $eval{$rollno} += $marks;         # Add $marks to $eval{$rollno}
  }

  foreach $k (keys %eval){
    print $k, ":", $eval{$k}, "\n";
  }

The function split can also take another argument, which is a pattern describing the delimiter between fields. For instance, the following line splits a line in which values are separated by commas, where we want to also ignore the spaces before and after commas.

   @values = split /\s*,\s*/,$line;


next up previous contents
Next: Writing functions Up: Programming in Perl Previous: Matching and regular expressions   Contents
Madhavan Mukund 2004-04-29