当前位置:首页 » Perl » Perl哈希

Perl哈希变量

Perl哈希变量,实例,使用例子在线教程:哈希是一种先进的数组形式

哈希是一种先进的数组形式。数组的限制之一是,它所包含的信息是很难获得的。例如,想象一下,你有一个人的名单和他们的年龄.

哈希解决了这个问题,非常整齐,让我们的访问,不是由一个指数,而是由一个标量的关键,@ages数组。例如使用不同的人的年龄,我们可以使用他们的名字作为重点来定义一个哈希.

%ages = ('Martin' => 28,
         'Sharon' => 35,
         'Rikke' => 29,);

print "Rikke is $ages{Rikke} years old\n";

This will produce following result
Rikke is 29 years old

创建哈希

哈希创建两种方式之一。在第一,你分配一个由一对一的基础上,以一个名为键的值:

$ages{Martin} = 28;

在第二种情况下,你使用的名单,这是由个人对从列表转换:对第一个元素被用作键和第二作为值。例如,

%hash = ('Fred' , 'Flintstone', 'Barney', 'Rubble');

为清楚起见,你可以使用=>一个别名,表示键/值对:

%hash = ('Fred' => 'Flintstone',
         'Barney' => 'Rubble');

提取单个元素

你可以从散列中提取单个元素,通过指定为你想要的值,括号内的关键字:

print $hash{Fred};

This will print following result
Flintstone

提取切片

您可以提取哈希片,就像你可以从一个数组中提取切片。然而,你需要使用@前缀,因为返回值将是一个对应值的列表:

#!/uer/bin/perl

%hash = (-Fred => 'Flintstone', -Barney => 'Rubble');
print join("\n",@hash{-Fred,-Barney});

This will produce following result
Flintstone
Rubble

: 使用$hash{-Fred, -Barney} 将无任何返回.

提取键和值

你可以通过使用密钥哈希键的列表:

#!/usr/bin/perl 

%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
print "The following are in the DB: ",join(', ',values %ages),"\n";

This will produce following result
The following are in the DB: 29, 28, 35

这些可以是有用的循环中,当你想打印所有哈希的内容:

#!/usr/bin/perl

%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
foreach $key (%ages)
{
  print "$key is $ages{$key} years  old\n";
}

This will produce following result  - www.gitbook.net
Rikke is 29 years  old
29 is  years  old
Martin is 28 years  old
28 is  years  old
Sharon is 35 years  old
35 is  years  old

这种方法的问题是,(%age)返回一个值的列表。因此,要解决这个问题,我们每个函数将返回键和值对,下面将给出

#!/usr/bin/perl

%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
while (($key, $value) = each %ages)
{
  print "$key is $ages{$key} years old\n";
}

This will produce following result
Rikke is 29 years old
Martin is 28 years old
Sharon is 35 years old

检测存在

如果你尝试访问哈希未含存在的键/值对,你通常未定义的值,如果你有开启的警告,然后你在运行时产生的警告。通过使用存在的函数,返回true,如果存在命名的键,不论它的价值可能是什么,你可以解决这个问题:

#!/usr/bin/perl

%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
if (exists($ages{"mohammad"}))
{
  print "mohammad if $ages{$name} years old\n";
}
else
{
  print "I don't know the age of mohammad\n - by www.gitbook.net";
}

This will produce following result
I don't know the age of mohammad

排序/排列哈希

没有办法简单地保证键,值,或键/值对列表的顺序将永远是相同的。事实上,它不是最好的甚至要依靠计算两个连续之间的顺序:

#!/usr/bin/perl

print(join(', ',keys %hash),"\n");
print(join(', ',keys %hash),"\n");

如果你想保证顺序,使用排序,例如:

  print(join(', ',sort keys %hash),"\n");

如果您正在访问哈希多次,要使用相同的顺序,考虑建立一个单一的数组来保存排序的序列,然后使用数组(这将留在排序顺序)遍历哈希。例如:

my @sortorder = sort keys %hash;
foreach my $key (@sortorder)

哈希大小

你的大小 - 使用任一键或值的标量上下文从一个哈希 - 也就是说,元素的数目:

#!/usr/bin/perl

%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
print "Hash size: ",scalar keys %ages,"\n";

This will produce following result
Hash size: 3

在哈希表中添加和删除元素

可以做一个使用简单的赋值操作符的代码行添加一个新的键/值对。但是从哈希中删除一个元素,你需要使用删除功能.

#!/usr/bin/perl

%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);

# Add one more element in the hash
$age{'John'} = 40;
# Remove one element from the hash
delete( $age{'Sharon'} );