The keys function returns, in random order, an array whose elements are the keys of a hash (see "The values Function" and "The each Function").
The values function returns, in random order, an array consisting of all the values of a hash.
Formatvalues(ASSOC_ARRAY) values ASSOC_ARRAY Example 5.54.
|
The each function returns, in random order, a two-element array whose elements are the key and the corresponding value of a hash.
Formateach(ASSOC_ARRAY) Example 5.55.
|
When sorting a hash, you can sort the keys alphabetically very easily by using the built-in sort() command, as we did with arrays in the preceding section. But you may want to sort the keys numerically or sort the hash by its values. To do this requires a little more work. You can define a subroutine to compare the keys or values. (See subroutines in Chapter 11). The subroutine will be called by the built-in sort() function. It will be sent a list of keys or values to be compared. The comparison is either an ASCII (alphabetic) or a numeric comparison, depending upon the operator used. The "cmp" operator is used for comparing strings, and the "<=>" operator is used for comparing numbers. The reserved global scalars $a, and $b are used in the subroutine to hold the values as they are being compared. The names of these scalars cannot be changed.
To perform an Ascii or alphabetic sort on the keys in a hash is relatively easy. The sort() function is given a list of keys and returns them sorted in ascending order. A foreach loop is used to loop through the hash one key at a time. See Example 5.56.
(In Script)
1 %wins = (
"Portland Panthers" => 10,
"Sunnyvale Sluggers" => 12,
"Chico Wildcats" => 5,
"Stevensville Tigers" => 6,
"Lewiston Blazers" => 11,
"Danville Terriors" => 8,
);
print "\n\tSort Teams in Ascending Order:\n\n";
2 foreach $key( sort(keys %wins)) {
3 printf "\t% -20s%5d\n", $key, $wins{$key};
}
(Output)
Sort Teams in Ascending Order:
Chico Wildcats 5
Danville Terriors 8
Lewiston Blazers 11
Portland Panthers 10
Stevensville Tigers 6
Sunnyvale Sluggers 12Explanation
|
To sort a hash by keys alphabetically and in descending order, just add the built-in reverse() function to the previous example. The foreach loop is used to get each key from the hash, one at a time, after the reversed sort.
1 %wins = (
"Portland Panthers" => 10,
"Sunnyvale Sluggers" => 12,
"Chico Wildcats" => 5,
"Stevensville Tigers" => 6,
"Lewiston Blazers" => 11,
"Danville Terriors" => 8,
);
print "\n\tSort Teams in Descending/Reverse Order:\n\n";
2 foreach $key (reverse sort(keys %wins)) {
3 printf "\t% -20s%5d\n", $key, $wins{$key};
}
(Output)
Sort Teams in Descending/Reverse Order:
Sunnyvale Sluggers 12
Stevensville Tigers 6
Portland Panthers 10
Lewiston Blazers 11
Danville Terriors 8
Chico Wildcats 5Explanation
|
A user-defined subroutine is used to sort a hash by keys numerically. In the subroutine, Perl's special $a and $b variables are used to hold the value being compared with the appropriate operator. For numeric comparison, the <=> operator is used, and for string comparison, the "cmp" operator is used. The sort() function will send a list of keys to the user-defined subroutine. The sorted list is returned.
|
Code View: 1 sub desc_sort_subject { 2 $b <=> $a; # Numeric sort descending } 3 sub asc_sort_subject{ 4 $a <=> $b; # Numeric sort ascending } 5 %courses = ( "101" => "Intro to Computer Science", "221" => "Linguistics", "300" => "Astronomy", "102" => "Perl", "103" => "PHP", "200" => "Language arts", ); print "\n\tCourses in Ascending Numeric Order:\n"; 6 foreach $key (sort asc_sort_subject(keys(%courses))) { 7 printf "\t%-5d%s\n", $key, $courses{"$key"}; } print "\n\tCourses in Descending Numeric Order:\n"; foreach $key (sort desc_sort_subject(keys(%courses))) { printf "\t%-5d%s\n", $key, $courses{"$key"}; } (Output) Courses in Ascending Numeric Order: 101 Intro to Computer Science 102 Perl 103 PHP 200 Language arts 221 Linguistics 300 Astronomy Courses in Descending Numeric Order: 300 Astronomy 221 Linguistics 200 Language arts 103 PHP 102 Perl 101 Intro to Computer Science Explanation
|
To sort a hash by its values, a user-defined function is also defined. The values of the hash are compared by the special variables $a and $b. If $a is on the left-hand side of the comparison operator, the sort is in ascending order, and if $b is on the left-hand side, then the sort is in descending order. The <=> operator compares its operands numerically.
(In Script) 1 sub asc_sort_wins { 2 $wins{$a} <=> $wins{$b}; } 3 %wins = ( "Portland Panthers" => 10, "Sunnyvale Sluggers" => 12, "Chico Wildcats" => 5, "Stevensville Tigers" => 6, "Lewiston Blazers" => 11, "Danville Terriors" => 8, ); print "\n\tWins in Ascending Numeric Order:\n\n"; 4 foreach $key (sort asc_sort_wins(keys(%wins))) { 5 printf "\t% -20s%5d\n", $key, $wins{$key}; } (Output) Wins in Ascending Numeric Order: Chico Wildcats 5 Stevensville Tigers 6 Danville Terriors 8 Portland Panthers 10 Lewiston Blazers 11 Sunnyvale Sluggers 12 Explanation
|
To sort a hash numerically and in descending order by its values, a user-defined function is defined as in the previous example. However, this time the $b variable is on the left-hand side of the <=> numeric operator, and the $a variable is on the right-hand side. This causes the sort() function to sort in descending order.
|
Code View: (In Script) # Sorting a Hash by Value in Descending Order 1 sub desc_sort_wins { 2 $wins{$b} <=> $wins{$a}; # Reverse $a and $b } 3 %wins = ( "Portland Panthers" => 10, "Sunnyvale Sluggers" => 12, "Chico Wildcats" => 5, "Stevensville Tigers" => 6, "Lewiston Blazers" => 11, "Danville Terriors" => 8, ); print "\n\tWins in Descending Numeric Order:\n\n"; 4 foreach $key (sort desc_sort_wins(keys(%wins))) { 5 printf "\t% -20s%5d\n", $key, $wins{$key}; } (Output) Wins in Descending Numeric Order: Sunnyvale Sluggers 12 Lewiston Blazers 11 Portland Panthers 10 Danville Terriors 8 Stevensville Tigers 6 Chico Wildcats 5 Explanation
|
The delete function deletes a value from a hash. The deleted value is returned if successful.[8]
[8] If a value in an %ENV hash is deleted, the environment is changed. (See "The %ENV Hash" on page 129.)
Formatdelete $ASSOC_ARRAY{KEY}Example 5.61.
|
The exists function returns true if a hash key (or array index) has been defined, and false if not.
Formatexists $ASSOC_ARRAY{KEY}Example 5.62.
|