Associative Arrays
CHAPTER 8: ASSOCIATIVE ARRAYS
What Is An Associative Array?
- Collection of scalar data
- Elements have no particular order
- Index values are arbitrary scalars and not necessarily integers,
as in an array
- Index values are called keys
Associative Array Variables
- Variable that holds a single associative array
- Name begins with a %
- Usually an associative array is created and accessed by referring
to its elements
- Associative array variables have their own namespace
Associative Array Assignment Operator (=)
- Used to give a value to an element of an associative array variable
- Associative array variables have the empty list value before they
are first assigned
- Ex.
$names{"bob"} = "tarr"; # Key "bob", value "tarr"
$ssn{"bob"} = 123456789; # Key "bob", value 123456789
- Note use of {} around the key
- Accessing an element that does not exist returns the undef value
Associative Array Literals
- An associative array literal is represented as a list of key-value
pairs
- Ex.
%names = ("bob", 1, "joe", 2);
$x{"bob"} = 1;
$x{"joe"} = 2; # %x is same as %names
@y = %x; # @y is ("bob", 1, "joe", 2)
%z = @y; # %z is same as %x
$w = %x; # $w is a junk string! Can NOT
# assign an associative array to
# a scalar in any meaningful way
Associative Array Variable Interpolation
- Elements of an associative array are interpolated in
a double-quoted string
- BUT an entire associative array can NOT be interpolated in a
double-quoted string
- Ex.
$x{"bob"} = 1;
$y = "bob";
print ("$x{$y}\n"); # Prints: 1
print ("%x\n"); # Prints: %x
Keys Function
- Gives a list of the keys of an associative array when used in an
array context. (Gives the empty list, if the associative array
is empty.)
- Gives the number of elements (key-value pairs) when used in a
scalar context. (Gives the value 0, if the associative array
is empty.)
- The list of keys is not returned in sorted order, but rather in
an (seemingly) random order determined by the internal way Perl
stores an associative array
- Ex.
$x{"bob"} = 10;
$x{"joe"} = 20;
@y = keys(%x); # @y is ("bob", "joe")
$z = keys(%x); # $z is 2
- Ex.
foreach $key (keys %x)
{
print ("Key $key has value $x{$key}\n");
}
Values Function
- Gives a list of the values of an associative array when used in
an array context. (Gives the empty list, if the associative
array is empty.)
- Gives the number of elements (key-value pairs) when used in a
scalar context. (Gives the value 0, if the associative array
is empty.)
- Ex.
$x{"bob"} = 10;
$x{"joe"} = 20;
@y = values(%x); # @y is (10, 20)
$z = values(%x); # $z is 2
Each Function
- Gives an element (key-value pair) of an associative array as a
two-element list when used in an array context. (Gives the empty
list, if the associative array is empty.)
- Each associative array has a corresponding iterator. On every use
of the each operator, the iterator advances to the next key-value
pair. When the end of the associative array is reached, the empty
list is returned. The next use of each after that starts the
process over.
- The iterator can be reset ONLY by reading all the elements from the
array or when a new value is assigned to the ENTIRE array
- Adding or deleting elements of the array should not be done while
using the iterator
- Gives just the next value when used in a scalar context.
- Ex.
$x{"bob"} = 1;
$x{"joe"} = 2;
while (($name, $numb) = each (%x))
{
print ("The number for $name is $numb\n");
}
Delete Function
- Used to delete an element (key-value pair) of an associative array
- Returns the element as a two-element list
- Ex.
$x{"bob"} = 1;
$x{"joe"} = 2;
delete ($x{"bob"});
Bob Tarr
University of Maryland, Baltimore County
tarr@umbc.edu