Perl教程
Perl控制语句
Perl高级

Perl 哈希

Perl 哈希

哈希是 perl 语言中最重要和最有影响力的部分。散列是一组键值对。键是唯一的字符串,值是标量值。
哈希是使用 my 关键字声明的。变量名以(%) 符号开头。
散列类似于数组,但它们之间有两个不同之处。第一个数组是有序的,但散列是无序的。其次,散列元素使用其值访问,而数组元素使用其索引值访问。
散列中不允许重复键,这使得键值在散列中是唯一的。每个键都有其唯一的值。
语法:
my %hashName = (
    "key" => "value";
)

Perl 哈希访问

要访问哈希的单个元素,在变量名前使用($)符号。然后关键元素写在 {} 大括号内。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "Australia"  => "Canberra"
);
print"$capitals{'India'}\n";
print"$capitals{'South Korea'}\n";
print"$capitals{'USA'}\n";
print"$capitals{'Australia'}\n";
输出:
     new Delhi
Seoul
Washington, D.C.
Canberra

Perl 哈希索引

哈希使用 $key 和 $value 变量进行索引。所有哈希值都将使用 while 循环打印。随着 while 循环的运行,每个变量的值都会被打印出来。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "Australia"  => "Canberra"
);
# LOOP THROUGH IT
while (($key, $value) = each(%capitals)){
     print$key.", ".$value."\n";
}
输出:
Australia, Canberra
India, new Delhi
USA, Washington, D.C.
South Korea, Seoul

Perl 按键对哈希进行排序

您可以使用其键元素或值元素对哈希进行排序。 Perl 为此提供了一个 sort() 函数。在此示例中,我们将按其关键元素对哈希进行排序。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "Australia"  => "Canberra"
);
# Foreach loop
foreach $key (sort keys %capitals) {
     print"$key: $capitals{$key}\n";
}
输出:
Australia: Canberra
India: new Delhi
South Korea: Seoul
USA: Washington: D.C.
看输出,所有的关键元素都是按字母顺序排列的。

Perl 按哈希值排序

这里我们将按哈希值元素排序。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "UK"  => "London"
);
# Foreach loop
foreach $value (sort {$capitals{$a} cmp $capitals{$b} }
           keys %capitals)
{
     print"$value $capitals{$value}\n";
}
输出:
UK London
India new Delhi
South Korea Seoul
USA Washington D.C.
看输出,所有的值元素都是按字母顺序排列的。

Perl 哈希键存在

从不存在的哈希中访问键值对将返回错误或警告。为了防止这种情况,您可以使用 exists() 函数检查散列中是否存在键。如果键存在则返回真。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "Australia"  => "Canberra"
);
if (exists($capitals{'India'}))
{
  print"found the key\n";
}
输出:
found the key
上面的输出表明'Indis'键存在于'capitals'散列中。

Perl Hash Slices

如果您只需要哈希中的一些值,您可以提取它们并显示为值列表。
为此,您必须将它们存储在一个带有 @ 前缀的数组变量中,因为它们将返回一个值列表,然后打印出来。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "Australia"  => "Canberra"
);
@array = @capitals{India, USA, Australia};
print"@array\n";
输出:
     new Delhi Washington, D.C. Canberra

Perl Hash 创建空散列

空散列的大小总是为 0。
在这个例子中,首先我们创建了一个散列大小为 3、然后我们创建了一个大小为 0 的空哈希。
my %first = ('john'=>9853147320, 'jose'=>7823654028, 'janie',=>'8850279610');  
print'hash size: ', scalar keys %first;  
print"\n";  
#creating emptyempty hash  
my %empty=();  
print'hash size: ', scalar keys %empty;
输出:
hash size: 3
hash size: 0

Perl 添加散列元素

新的键值对可以通过将它们声明为散列变量中的单个元素来添加到散列中。
在这里,我们添加了两个键值对,[德国-柏林] 和 [英国-伦敦]。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "Australia"  => "Canberra"
);
while (($key, $value) = each(%capitals)){
     print$key.", ".$value."\n";
}
#adding new element
$capitals{Germany} = Berlin;
$capitals{UK} = London;
# Printing new hash
print"\n";
while (($key, $value) = each(%capitals)){
     print$key.", ".$value."\n";
}
输出:
UK, London
Australia, Canberra
Germany, Berlin
India, new Delhi
USA, Washington D.C.
South Korea, Seoul

Perl 删除散列元素

要删除散列元素,请使用 delete() 函数。
这里,我们已经删除了在上一个示例中添加的两个键值对。
my %capitals = (
    "India"  => "New Delhi",
    "South Korea" => "Seoul",
    "USA"  => "Washington, D.C.",
    "Australia"  => "Canberra"
    "Germany "  => " Berlin"
    " UK "  => "London"
);
while (($key, $value) = each(%apitals)){
     print$key.", ".$value."\n";
}
#removing element
delete($capitals{Germany});
delete($capitals{UK});
# Printing new hash
print"\n";
while (($key, $value) = each(%capitals)){
     print$key.", ".$value."\n";
}
输出:
Australia, Canberra
India, new Delhi
USA, Washington D.C.
South Korea, Seoul

Perl 删除与未定义散列元素的对比

删除: 删除时,键值对将从散列中删除。
语法:
delete($hash{$key});
undef: 在 undef 中,值将是未定义的,但键将保留在哈希中。
语法:
Undef $hash{$key};
在以下示例中,我们创建了一个散列"等级"。我们将一一取消定义并从散列中删除所有键值。取消定义一个键时,只会显示它的值,删除一个键时,它会连同它的值一起从散列中完全删除。
这样,最后所有散列元素都将被删除。
# creating hash
$rank{'John'} = '5 ';
$rank{'Ana'} = '7 ';
$rank{'Jiyaa'} = '3 ';
$rank{'Jassi'} = '1 ';
# printing all elements in the hash
print"before:\n";
print%rank;
print"\n";
# undefining John in the hash
print"\nundefine John\n";
undef$rank{'John'};
print%rank;
print"\n";
# deleting the 'John' key the in hash element
print"\nremove John\n";
delete($rank{'John'});
print%rank;
print"\n";
# undefining Ana in the hash
print"\nundefine Ana\n";
undef$rank{'Ana'};
print%rank;
print"\n";
# deleting the 'Ana' key the in hash element
print"\nremove Ana\n";
delete($rank{'Ana'});
print%rank;
print"\n";
# undefining Jiyaa in the hash
print"\nundefine Jiyaa\n";
undef$rank{'Jiyaa'};
print%rank;
print"\n";
# deleting the 'Jiyaa' key the in hash element
print"\nremove Jiyaa\n";
delete($rank{'Jiyaa'});
print%rank;
print"\n";
# undefining Jassi in the hash
print"\nundefine Jassi\n";
undef$rank{'Jassi'};
print%rank;
print"\n";
# deleting the 'Jassi' key the in hash element
print"\nremove Jassi\n";
delete($rank{'Jassi'});
print%rank;
输出:
before:
John5 Jassi1 Jiyaa3 Ana7
undefine John
JohnJassi1 Jiyaa3 Ana7
remove John
Jassi1 Jiyaa3 Ana7
undefine Ana
Jassi1 Jiyaa3 Ana
remove Ana
Jassi1 Jiyaa3
undefine Jiyaa
Jassi1 Jiyaa
remove Jiyaa
Jassi1
undefine Jassi 
Jassi
remove Jassi
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4