lecture in color

References and structures (PP chapters 8,9)

Solution: references

Shorthands

Comparing References, Arrays, and Hashes

ArrayRef to arrayHashRef to hash
@a = (1,2,3); $a = [1,2,3]; %h = ('a'=>1,'b'=>2); $h = {'a'=>1,'b'=>2};
$a[1] ${$a}[1] or $a->[1] $h{'a'} ${$h}{'a'} or $h->{'a'}
$a[5]=7; $a->[5]=7; $h{'c'}=9; $h->{'c'}=9;
$#a $#{$a} keys %h keys %$h
@a @$a %h %$h

Why references?

Example: structs in Perl

$s = { 'name' => 'Bill Clinton', 
       'email' => 'president@whitehouse.gov', 
       'title' => 'President of the United States'
     };

print "$s->{'name'} has email $s->{'email'}\n"; 

Easy references

Nesting Arrays

Nesting Structures

Another shorthand:

Example: two-dimensional arrays.

Example: two-dimensional hashes:

Data::Dumper

Example of Dumper

A more advanced trial

Typical uses

Fast Set Membership


lecture in color


downloaded on Nov-23-2009 04:04:56 PM,
was last modified on Dec-31-1969 07:00:00 PM.

All lecture note content is copyright 2003 by
Alva L. Couch, Computer Science, Tufts University
(couch at cs dot tufts dot edu)