Perl中的哈希排序
先决条件:Perl |哈希
一组键/值对称为哈希。散列结构中的每个键都是唯一的,并且属于字符串类型。与这些键关联的值是标量。这些值可以是数字、字符串或引用。 Hash 是使用 my 关键字声明的。让我们考虑一个例子来理解对哈希排序的概念。
示例:考虑一个包含班级学生姓名和平均分数的哈希。在这里,学生的名字是键,他们的平均分是值。使用foreach 循环在keys
函数返回的学生姓名上打印此哈希。
# Perl program to demonstrate
# the concept of hash
use strict;
use warnings;
use 5.010;
# Creating a hash of studentnames
# and their average score
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
# displaying the keys and values of Hash
# using the foreach loop and keys function
# output may be different each
# time when you run the code
foreach my $name (keys %studentnames) {
# printing the keys and values of hash
printf "%-8s %s\n", $name, $studentnames{$name};
}
输出:
Marty 16.5
Jason 25.2
Plato 39
Vivek 27
Socrates 29.5
Martha 14
Earl 31
Uri 19.6
Nitin 30
注意:输出可能包含任何随机顺序,具体取决于系统和 Perl 的版本。
哈希可以通过多种方式排序,如下所示:
- 根据其键的 ASCII 值对 Hash 进行排序:通常,排序是基于 ASCII 表。这意味着排序会将所有大写字母保持在所有小写字母的前面。这是排序的默认行为。上面的示例代码是根据其键的 ASCII 值排序的。
- 根据其键的字母顺序对 Hash 进行排序:这里,键是按字母顺序排序的。
例子:
# Perl program to demonstrate # sorting of the hash according # alphabetical order of its keys use strict; use warnings; use 5.010; # Creating a hash of studentnames # and their average score my %studentnames = ( Martha => 14, Vivek => 27, Earl => 31, Marty => 16.5, Jason => 25.2, Socrates => 29.5, Uri => 19.6, Nitin => 30, Plato => 39, ); # sorting the hash according # alphabetical order of its keys foreach my $name (sort {lc $a cmp lc $b} keys %studentnames) { printf "%-8s %s\n", $name, $studentnames{$name}; }
输出:
Earl 31 Jason 25.2 Martha 14 Marty 16.5 Nitin 30 Plato 39 Socrates 29.5 Uri 19.6 Vivek 27
- 根据哈希值排序:您也可以根据哈希值对哈希值进行排序,如下所示:
示例 1:根据 ASCII 表对哈希值进行排序。
# Perl program to demonstrate # sorting of the hash according # to the values of Hash use strict; use warnings; use 5.010; # Creating a hash of studentnames # and their average score my %studentnames = ( Martha => 14, Vivek => 27, Earl => 31, Marty => 16.5, Jason => 25.2, Socrates => 29.5, Uri => 19.6, Nitin => 30, Plato => 39, ); # sorting of the hash according # to ASCII code of values of Hash foreach my $name (sort values %studentnames) { say $name; }
输出:
14 16.5 19.6 25.2 27 29.5 30 31 39
示例2:按照Hash的Values的数值排序如下:
# Perl program to demonstrate # sorting of the hash according # to the values of Hash use strict; use warnings; use 5.010; # Creating a hash of studentnames # and their average score my %studentnames = ( Martha => 14, Vivek => 27, Earl => 31, Marty => 16.5, Jason => 25.2, Socrates => 29.5, Uri => 19.6, Nitin => 30, Plato => 39, ); # sorting of the hash according # to the numerical value of # Values of hash foreach my $name (sort {$a<=>$b} values %studentnames) { say $name; }
输出:
14 16.5 19.6 25.2 27 29.5 30 31 39
- 根据值对哈希的键进行排序:您还可以根据给定的值对哈希的键进行排序。
示例 1:在下面的程序中,<=> 被称为spaceship 运算符 。如果您将一次又一次地运行代码,那么您会注意到输出的差异。有时你会在Nitin之前找到柏拉图,反之亦然。
# Perl program to demonstrate the # Sorting of keys of the hash # according to the values use strict; use warnings; use 5.010; # Creating a hash of studentnames # and their average score my %studentnames = ( Martha => 14, Vivek => 27, Earl => 31, Marty => 16.5, Jason => 25.2, Socrates => 29.5, Uri => 19.6, Nitin => 45, Plato => 45, ); # Sort the keys of the hash # according to the values # Here $a and $b are the # placeholder variable of sort foreach my $name (sort {$studentnames{$a} <=> $studentnames{$b}} keys %studentnames) { printf "%-8s %s\n", $name, $studentnames{$name}; }
输出:
Martha 14 Marty 16.5 Uri 19.6 Jason 25.2 Vivek 27 Socrates 29.5 Earl 31 Plato 45 Nitin 45
例2:为解决上述代码,具有相同值的键可以根据ASCII表排序如下:
# Perl program to demonstrate the # Sorting of keys of the hash # according to the values use strict; use warnings; use 5.010; # Creating a hash of studentnames # and their average score my %studentnames = ( Martha => 14, Vivek => 27, Earl => 31, Marty => 16.5, Jason => 25.2, Socrates => 29.5, Uri => 19.6, Nitin => 45, Plato => 45, ); # keys that have the same value # will be sorted according the # ASCII table foreach my $name (sort { $studentnames{$a} <=> $studentnames{$b} or $a cmp $b } keys %studentnames) { printf "%-8s %s\n", $name, $studentnames{$name}; }
输出:
Martha 14 Marty 16.5 Uri 19.6 Jason 25.2 Vivek 27 Socrates 29.5 Earl 31 Nitin 45 Plato 45
说明:这里可以看到键Nitin和Plato是按照 ASCII 表排序的。无论您运行代码多少次,输出都将保持不变。