Title: Hashes
1Hashes
- CSC 492 Topics in Perl
- Joe Reynoldson
- 1/31/05
2Hashes
- An unordered list of values indexed by keys
- Often referred to as 'associative arrays'
- Can be any size
- Keys must be unique strings
- Values are not necessarily unique
3Hashes Store Related Data
- driver's license number gt name
- What is the name of the person with a given
license number? - host name gt IP address
- What is the hostname of a machine with a given IP
address? - word gt word count
- How many times did a particular word appear in a
document? - username gt quota usage
- How much disk space is a particular username
using?
4Accessing Individual Hash Elements
hash_namekey value
- This example assigns a value to a key in the hash
hash_name - The string key is 'associated' with the string
value, hence the phrase 'associative array'
my value hash_namekey
- This example reads a value associated with a key
in the hash named hash_name - Adding key/value pairs to a hash, and reading
values from a hash is very similar to an array
5More on key/value pairs
- New key/value pairs are automatically created as
the result of an assignment - Assigning to an existing key overwrites the
associated value - Trying to read a nonexistent key/value pair will
return undef - Scalar representations of hashes are interpolated
in double quoted strings
6Example of Individual Hash Elements
- !/usr/bin/perl -w
- A hash of usernames indexed by userids
- use strict
- my username
- create a hash with 1 element
- username'796' 'jreynold'
- the following assignment assigns undef to
this_user - my this_user username'797'
- replace username jreynold with joe
- username'796' 'joe'
- prints joe, and nothing else
- print "username'796', this_user\n"
- prints username
Output joe, username
7The hash, the whole hash, and nothing but the hash
- A hash can be initialized using a list of
key/value pairs - The order of keys and values will be random
- Using the 'big arrow' makes the pairs easier to
read, and the big arrow is equivalent to a comma - Assigning a hash to an array 'unwinds' the hash,
meaning the alternating list of key/value pairs
will be assigned to the array
my username qw/ 796 jreynold 797 djennewe /
my username ( '796' gt 'jreynold',
'797' gt 'djennewe' )
my _at_unwound_hash hash
8Hash Functions
- The keys function will return all of the keys of
the hash in a random order - The list of keys is guaranteed to be unique
- This function is commonly used to iterate over
all key/value pairs in a hash - The keys function in a scalar context returns the
number key/value pairs - The values function will return all of the values
stored in the hash in a random order - The keys and values functions return an empty
list for an empty hash
9(important) Example of hash iteration
- !/usr/bin/perl -w
- use strict
- my quota ( 'joe' gt '7MB',
- 'jreynold' gt '200MB',
- 'jdoe' gt '12MB' )
- foreach my uname (keys quota)
- print "uname is using quotauname\n"
Output jdoe is using 12MB jreynold is using
200MB joe is using 7MB
10Example of sorted hash iteration
Output jdoe is using 12MB joe is using 7MB
jreynold is using 200MB
- !/usr/bin/perl -w
- use strict
- my quota ( 'joe' gt '7MB',
- 'jreynold' gt '200MB',
- 'jdoe' gt '12MB' )
- foreach my uname (sort( keys quota ))
- print "uname is using quotauname\n"
112 more hash functions
- The exists function returns true if a key exists
in a hash, regardless of the associated value - This is handy when a key may exist but currently
is associated with an undef value - The delete function deletes a corresponding
key/value pair - Doesn't associate undef with the key
- Removes the key/value pair entirely
12An Existential Hash Example
- !/usr/bin/perl -w
- A lesson in truth and existence
- use strict
- my username
- username'798' undef
- if( username'798' ) print "798 is true\n"
- else print "798 is false\n"
- if( defined( username'798' ) ) print "798 is
defined\n" - else print "798 is not defined\n"
- if( exists( username'798' ) ) print "798
exists\n" - else print "798 does not exist\n"
Output 798 is false 798 is not defined 798 exists