Perl Programming Page 3 - Hash Functions |
Since hashes in list context are apparently random collections of key/value pairs, we can’t really use foreach loops on them directly. If we did, we would get both keys and values with no indication as to which was which. To help us, Perl provides three functions for iterating over hashes:keys(),values(), and Also, Perl provides functions to remove elements (delete(), already seen previously), and to check to see if a key exists in the hash (exists()). The keys() Function First, there is keys(%hash). This gives us a list of the keys (all of the scalars on the left-hand side). This is usually what we want when we wish to visit each hash entry in turn as shown in this example: #!/usr/bin/perl -w use strict; my %where = ( foreach (keys %where) { Currently, this tells us $ perl keys.pl You may find that the output appears in a different order on your machine.1 Don’t worry. As mentioned before, hashes are unordered, and there’s no guarantee that the keys will come out in the same order each time. It really depends on the particular version of Perl that you are using. Let’s look at the part of the program that does all the work: foreach (keys %where) { keys()is a function which, likesort()andreverse(), returns a list. The list in this case is The values() Function The counterpart to keys() isvalues(), which returns a list of all of the values in the hash. This is somewhat less useful, since you can always find the value if you have the key, but you cannot easily find the key if you have the value. It’s almost always advantageous to usekeys()instead. Here is an example using thevalues()function: #!/usr/bin/perl -w use strict; my %where = ( foreach (values %where) { 1. Or even different every time that you run it! Some 5.8.x Perl installations have hash order randomization turned on by default. Executing this program produces the following: $ perl values.pl The each() Function The next hash function is each(). It returns each hash entry as a key/value pair. Normally, the values returned are copied into an assignable list like this: each() . It returns hash entry as a key/value pair. Normally, the values returned are copied into an assignable list like this:The next hash function is each(). It returns hash entry as a key/value pair. Normally, the values returned are copied into an assignable list like this: ($k, $v) = each %where; This is illustrated ineach.pl: #!/usr/bin/perl -w use strict; my %where = ( my($k, $v); Here is an example of this program executing: $ perl each.pl The delete() Function We have already seen the delete() function. It removes a key/value pair from a hash. This statement frombadhash.plremoves the pair Lucy/Exeter from delete $where{Lucy}; Since we are on the subject, we should mention that thedelete()function also deletes array elements. The following code would remove element 3 from the array@array. Note that the element returns to an uninitialized state: delete $array[3]; The exists() Function The last function we will look at is the exists() function. It returns true if the key exists in the hash, false if not. Here is an example: exists() function. It returns true if the key exists in the hash, false if not. Here is an example:The last function we will look at is the exists() function. It returns true if the key exists in the hash, false if not. Here is an example: #!/usr/bin/perl -w
my %where = ( print "Gary exists in the hash!\n" if exists $where{Gary}; Running this program results in the following: $ perl exists.pl Note exists()returns 1 when true, an empty string when false. Theexists()function also works for array elements. This code checks to see if element 3 exists in@array: if (exists $array[3]) {
blog comments powered by Disqus |
|
|
|
|
|
|
|