个性化阅读
专注于IT技术分析

Perl哈希用法详解

本文概述

哈希是perl语言中最重要和最有影响力的部分。哈希是一组键值对。键是唯一的字符串, 值是标量值。

哈希是使用我的关键字声明的。变量名称以(%)符号开头。

哈希就像数组, 但是它们之间有两个区别。第一个数组是有序的, 但哈希是无序的。其次, 使用其值访问哈希元素, 而使用其索引值访问数组元素。

哈希中不允许使用重复的键, 这会使键值在哈希值内唯一。每个键都有其单个值。

句法:

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()函数检查哈希中是否存在键。如果密钥存在, 则返回true。

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哈希片

如果只需要哈希中的某些值, 则可以提取它们并将其显示为值列表。

为此, 你必须将它们存储在带有@前缀的数组变量中, 因为它们将返回值列表, 然后打印它们。

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哈希创建空哈希

空散列的大小始终为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添加哈希元素

通过将新的键值对声明为hash变量中的单个元素, 可以将它们添加到hash中。

在这里, 我们添加了两个键值对, [德国-柏林]和[英国-伦敦]。

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
赞(0)
未经允许不得转载:srcmini » Perl哈希用法详解

评论 抢沙发

评论前必须登录!