Link | Description | Commands Used |
Hash Variables | Relation between lists and HashVariables | hash list %H = (r, 255,g,150,b,0); |
Conversion | Converting among arrays, lists, and hashes | @a = %h and %h = @a |
Accessing Hash Elements | Use of the Braces (Curly Brackets) | $h{'key'} = 1; |
Deleting Hash Elements | The delete and exists function | delete(), exists() |
Accessing all Hash values | Looping over all Hash Values | foreach, keys(), values(), each() |
Hash Contexts | Hash Contexts | ($v1, $v2, $v3) = %H; $x = %H; |
48. Hash Variables A list is a collection of scalars, and an Array is an ordered list, indexed by element. A hash is an unordered collection of pairs of scalars: keys and values. These value pairs are not in any order. A value in the hash is accessed by its key. This is sometimes referred to as an associative array. (The key is associated with the hash's value). As a result, you cannot refer to the first or last element of a hash; You cannot numerically iterate over the elements in that hash (But you can get a list of the hash's keys, of its values, or of both in pairs, and access all of the elements that way by iterating over those entities using a loop.) The symbol for a hash is % and it follows the same rules as array variables do. Remember %x is different from @x and is also different from $x Since the hash's are collections of pairs of values, when defined there must be an even number of elements: (keys, values). If there is an odd number of elements, the last value is ignored. %pairs = ('red', 255, 'green', 150, 'blue', 0); Since large hashes can look unweildy, this can be written using white space as: %pairs = ( 'red', 255, 'green', 150, 'blue', 0 ); The most preferred notation is to use the "corresponds to" operator =>, which acts just like the comma operator (and it quotes the word to its left.) %pairs = ( red => 255, green => 150, blue => 0 ); Hash keys are expected to be strings. Those containing spaces must be explicitly quoted. A Hash with no keys or values is defined by the null list. %hash = (); # no keys or values 49. Converting among Arrays, Lists and Hashes Another way to create a hash is to use an array or a list for its initial elements. Since hashes and arrays use lists as their base form, you can copy them back and forth: @stuff = ('one', 1, 'two', 2, 'three', 3); %pairs = @stuff; print %pairs, "\n"; @stuff = %pairs print @stuff, "\n" $ perl -e '$, = " "; @stuff = ('one', 1, 'two', 2, 'three', 3); %pairs = @stuff; print %pairs, "\n"; @stuff = %pairs; print @stuff, "\n";' three 3 two 2 one 1 three 3 two 2 one 1 50. Accessing Hash Elements To access or assign a value to a hash, use its key and braces to specify the value: $hash{'Seattle'} = 100; print $hash{'Seattle'}; This is analogous to the Array access syntax $array[subscript]; With single word keys, quotes are optional. What are these? $name @name %name $name[$index] $name{$key} What does this do? %pairs = ( red => 255, green => 150, blue => 0 ); $pairs{mauve} = 22; $, = " "; print %pairs, "\n"; 51. Deleting Hash Elements The delete () function is used to delete one element pair of a hash as follows: $hash2{$key} = delete $hash{$key}; To delete all the keys in a hash, use: %hash2 = (); To selectively delete some keys in a hash, use undef (faster): $hash2{$key} = undef $hash{$key1}; To test for a key's existence, use: if (exists $hash{$key}) { $hash{$key}++; } 52. Processing all the Values in a Hash The keys function has the form: keys (%hash); or keys %hash # Since only 1 argument The values function has the form: values (%hash); or values %hash # Since only 1 argument Suppose you wish an alphabetized list of hash keys with their associated values foreach $key (sort keys %hash) { print "$key: $hash{$key} valueunit\n"; } 53. Hashes in different Contexts List Context: @colors = %pairs; #results in an array of all elements ($x, $y, $z) = %pairs; #results in the 1st 3 elements of the unwound hash being # set to x, y and z variables, rest of elements ignored. $, = " "; print %pairs, "\n"; #results in a space separated list of key, value pairs Scalar Context: $x = %pairs # describes the internal state m/n of the hash table. m is the number of slots used by the data, n is the number of slots allocated. An efficient hash table doesn't allocate too much storage. For Example: open PASSWD, '/etc/passwd'; while (<PASSWD>) { chop; # Remove trailing newline ($login, $passwd, $uid, $gid, $gecos, $home, $shell) = split /:/; %account = ( login => $login, passwd => $passwd, uid => $uid, gid => $gid, gecos => $gecos, home => $home, shell => $shell }; }
Questions? Robert Katz: katz@cis.highline.ctc.edu