珀尔 |多维哈希
- 先决条件:哈希基础
介绍
除了散列的正常约束之外,我们还可以创建由两个组合组成的复杂结构。这些是嵌套或复杂的结构,它们可用于以易于使用的方式对复杂数据进行建模
格式。
在所有 Perl 的嵌套结构中,多维散列或散列散列是最灵活的。这就像建立一个本身包含一组其他记录的记录。
创建散列散列的格式类似于数组数组。简单地说,不是将值分配给普通散列中的主键,而是将包含辅助键及其各自值的整个散列分配给外部散列的主键。
Syntax:
my %hash = (primary_key => {secondary_key => {sub_sec_key => {…}}});
例子:
以下示例显示了描述公司组织的哈希值。主键是部门,嵌套键是员工姓名。然后这些值包含相应员工的职位。
# !/usr/bin/perl
# Perl program to demonstrate
# Multidimensional hash
use strict;
use warnings;
use Data::Dumper qw(Dumper);
# Creating a 2D hash
my %company = ('Sales' => {
'Brown' => 'Manager',
'Smith' => 'Salesman',
'Albert' => 'Salesman',
},
'Marketing' => {
'Penfold' => 'Designer',
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
},
'Production' => {
'Cotton' => 'Paste-up',
'Ridgeway' => 'Manager',
'Web' => 'Developer',
},
);
# Print the List
print Dumper(\%company);
$VAR1 = {
'Marketing' => {
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
'Penfold' => 'Designer'
},
'Sales' => {
'Smith' => 'Salesman',
'Albert' => 'Salesman',
'Brown' => 'Manager'
},
'Production' => {
'Ridgeway' => 'Manager',
'Web' => 'Developer',
'Cotton' => 'Paste-up'
}
};
在上面的示例中, Dumper函数的输入是对数据结构的引用,因此我们在 %company 前面放置了一个反斜杠(\) 。
注意:键的顺序是随机的,因为散列不会使它们保持任何特定的顺序。
其他一些操作:
- 将另一个匿名成员添加到现有哈希:
Syntax:
$company{‘new_key’} = {‘sub_key1’ => value1,
‘sub_key2’ => value2,
‘sub_key3’ => value3
}; - 访问特定值:
Syntax:
print $company{“Production”}{“Web”}; # will output “Developer” - 设置特定键的值:
Syntax:
$company{$Production}->{“Web”} = Senior Developer ; # changes Web to Senior Developer
遍历多维哈希
要遍历多维散列或遍历散列中的每个值,只需循环遍历外部散列的键,然后遍历内部散列的键。
对于 N 维散列,需要 N 个嵌套或嵌入的循环来遍历完整的散列。 For和While循环都可用于循环到散列。
句法:
for $key (keys %hash)
{
print "$key: \n";
for $ele (keys %{$hash{$key}})
{
print " $ele: " . $hash{$key}->{$ele} . "\n";
}
}
对于大型多维哈希,使用每个关键字同时检索键和值可能会稍微快一些。
句法:
while (($key, $ele) = each %hash)
{
print "$key: \n";
while (($ele, $sub_ele) = each %$ele)
{
print " $ele = $sub_ele ";
}
print "\n";
}
以下示例说明了使用For
和while
循环遍历多维散列:
例子:
# !/usr/bin/perl
# Perl program to demonstrate
# Traversing of
# Multidimensional hash
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my %company = ('Sales' => {
'Brown' => 'Manager',
'Smith' => 'Salesman',
'Albert' => 'Salesman',
},
'Marketing' => {
'Penfold' => 'Designer',
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
},
'Production' => {
'Cotton' => 'Paste-up',
'Ridgeway' => 'Manager',
'Web' => 'Developer',
},
);
print "Traversing hash using For loop: "."\n";
print "\n";
# traversing hash using for loop
for my $key (keys %company)
{
print "$key: \n";
for my $ele (keys %{$company{$key}})
{
print " $ele: " . $company{$key}->{$ele} . "\n";
}
}
print "\nTraversing hash using while" .
"loop using each keyword: " . "\n";
print "\n";
# traversing hash using each keyword
# and while loop
while ((my $key, my $ele) = each %company)
{
print "$key: \n";
while ((my $ele, my $sub_ele) = each %$ele)
{
print " $ele = $sub_ele ";
}
print "\n";
}
Traversing hash by For loop.
Marketing:
Evans: Tea-person
Jurgens: Manager
Penfold: Designer
Sales:
Brown: Manager
Albert: Salesman
Smith: Salesman
Production:
Cotton: Paste-up
Web: Developer
Ridgeway: Manager
Traversing hash using while loop with each keyword.
Marketing:
Evans=Tea-person Jurgens=Manager Penfold=Designer
Sales:
Brown=Manager Albert=Salesman Smith=Salesman
Production:
Cotton=Paste-up Web=Developer Ridgeway=Manager
检查多维哈希中的键是否存在
很多时候在使用 Perl 散列时,我们需要知道某个键是否已经存在于散列中。给定一个散列,可以使用exists
关键字检查特定键的存在。
在上述示例中使用的多维散列(如%company )中,必须使用关键字exists
,直到达到检查是否存在的键的深度级别。
Syntax:
if (exists($hash{key})) {
if (exists($hash{key}{sub_key})) {
….
}
}
还应该小心使用第 n 级构造而不首先尝试第 (n-1) 级,因为这可能会触发不需要的自动存活
例子:
这是一个演示 Perl exists
哈希函数的简单示例。在这个 Perl 脚本中,我们将首先创建一个简单的 Perl 散列,然后我们将使用exists
函数查看名为“Albert”的散列键是否存在于散列中。
# !/usr/bin/perl
# Perl program to check for
# existence of a key in a
# Multidimensional hash
use strict;
use warnings;
my %company = ('Sales' => {
'Brown' => 'Manager',
'Smith' => 'Salesman',
'Albert' => 'Salesman',
},
'Marketing' => {
'Penfold' => 'Designer',
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
},
'Production' => {
'Cotton' => 'Paste-up',
'Ridgeway' => 'Manager',
'Web' => 'Developer',
},
);
# Check for key existence
if (exists $company{"Sales"})
{
print "Sales department exists.\n";
if (exists $company{"Sales"}{"Albert"})
{
print "Albert is " . $company{"Sales"}{"Albert"} .
" of Sales department . \n";
}
else
{
print "Albert is not a member of Sales department.\n";
}
}
else
{
print "Sales department do not exists.\n";
}
Sales department exists.
Albert is Salesman of Sales department.