珀尔 |哈希
一组键/值对称为哈希。散列结构中的每个键都是唯一的,并且属于字符串类型。与这些键关联的值是标量。这些值可以是数字、字符串或引用。 Hash 是使用my关键字声明的。变量名前面是美元符号 ($) ,后面是花括号下的键和与键关联的值。每个键都与一个值相关联。
例子:
my%rateof{mango} = 45;
问题出现了何时使用数组以及何时使用哈希?
- 如果一切都井井有条,那就去阵列吧。例如:
1. A list of people in a bank queue.
2. A list of people in railway reservation line.
3. A list of files to read.
- 如果我们拥有的东西不正常,那么就去哈希。例如:
1. An index of surname looked up by the first name.
2. An index showing the size of files looked up by name.
空散列:没有任何键的散列变量称为空散列。
- 例子:
my %rateof;
- 这里的rateof是哈希变量。
将键/值对插入哈希:键始终为字符串类型,值始终为标量类型。
- 例子:
$rateof{'mango'} = 45;
- 这里的键是芒果,值是 45。
哈希的初始化和获取哈希的元素:哈希变量可以在其声明期间使用键/值对进行初始化。有两种方法可以初始化散列变量。一种是使用=> ,它被称为胖箭头或胖逗号。第二种是将键/值对放在用逗号(,) 分隔的双引号(“”)中。使用粗逗号提供了一种替代方法,因为您可以在键周围留下双引号。
要从散列中访问单个元素,您可以使用美元符号 ($),后跟散列变量,然后是花括号下的键。在访问键/值对时,如果您传递了其中不存在的键,那么它将返回一个未定义的值,或者如果您打开警告,它将显示警告。
- 例子:
Perl
# Perl program to demonstrate the
# Fetching an element of a Hash
# creating hash
%rateof = ('Mango' => 45, 'Orange' => 30, 'Grapes' => 40);
# Fetching an element of Hash
print "$rateof{'Mango'}\n";
print "$rateof{'Orange'}\n";
print "$rateof{'Grapes'}\n";
Perl
# Perl program to demonstrate the
# empty values of a Hash
#use warnings;
# creating hash
%rateof = ('Mango' => 45, 'Orange' => undef, 'Grapes' => 40);
# Fetching an element of Hash
print "$rateof{'Mango'}\n";
print "$rateof{'Orange'}\n";
print "$rateof{'Grapes'}\n";
Perl
# Perl program to find the size of a Hash
#use warnings;
# creating hash
%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23);
# creating array of keys
@keys = keys %rateof;
$size = @keys;
print "Hash size using Keys is: $size\n";
# creating hash of values
@values = values %rateof;
$size = @values;
print "Hash size using Values is: $size\n";
Perl
# Perl program to add an element in a hash
#use warnings;
# creating hash
%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23);
# array of keys
@keys = keys %rateof;
$size = @keys;
print "SIZE OF HASH BEFORE ADDING: is $size\n";
# Adding new key/value pair into hash
$rateof{'Potato'} = 20;
# array of keys
@keys= keys %rateof;
$size = @keys;
print "SIZE OF HASH AFTER ADDING: is $size\n";
Perl
# Perl program to element from hash
#use warnings;
# creating hash
%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23);
# creating array of keys
@keys= keys %rateof;
# finding size of hash
$size = @keys;
print "SIZE OF HASH BEFORE DELETING: $size\n";
# using delete function
delete $rateof{'Mango'};
# creating array of keys
@keys= keys %rateof;
# finding size of hash
$size = @keys;
print "SIZE OF HASH AFTER DELETING: $size\n";
- 输出:
45
30
40
- 说明:要访问 Mango、Orange、Grapes 三个键并打印与其关联的值,只需使用美元符号后跟 Key。 print 语句将打印与该键关联的值。
哈希中的空值:通常,您不能将空值分配给哈希的键。但是在 Perl 中,有另一种方法可以为 Hashes 提供空值。通过使用undef函数。 “undef”可以根据用户的需要分配给新的或现有的密钥。将 undef 放在双引号中将使其成为字符串而不是空值。
Perl
# Perl program to demonstrate the
# empty values of a Hash
#use warnings;
# creating hash
%rateof = ('Mango' => 45, 'Orange' => undef, 'Grapes' => 40);
# Fetching an element of Hash
print "$rateof{'Mango'}\n";
print "$rateof{'Orange'}\n";
print "$rateof{'Grapes'}\n";
输出:
45
40
遍历散列:要访问散列中的值,用户必须知道与该值关联的键。如果之前不知道哈希的键,那么在键函数的帮助下,用户可以获得键列表并可以遍历这些键。
例子:
my @fruits = keys %rateof;
for my $fruit (@fruits) {
print "The color of '$fruit' is $rateofof{$fruit}\n";
}
哈希大小:键/值对的数量称为哈希大小。要获得大小,第一个用户必须创建一个键或值数组,然后他可以获得数组的大小。
- 句法:
print scalar keys % hash_variable_name;
- 例子:
Perl
# Perl program to find the size of a Hash
#use warnings;
# creating hash
%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23);
# creating array of keys
@keys = keys %rateof;
$size = @keys;
print "Hash size using Keys is: $size\n";
# creating hash of values
@values = values %rateof;
$size = @values;
print "Hash size using Values is: $size\n";
- 输出:
Hash size using Keys is: 4
Hash size using Values is: 4
在散列中添加和删除元素:用户可以使用简单的赋值运算符轻松地将一对新的键/值添加到散列中。
- 示例 1:在哈希中添加元素
Perl
# Perl program to add an element in a hash
#use warnings;
# creating hash
%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23);
# array of keys
@keys = keys %rateof;
$size = @keys;
print "SIZE OF HASH BEFORE ADDING: is $size\n";
# Adding new key/value pair into hash
$rateof{'Potato'} = 20;
# array of keys
@keys= keys %rateof;
$size = @keys;
print "SIZE OF HASH AFTER ADDING: is $size\n";
- 输出:
SIZE OF HASH BEFORE ADDING: is 4
SIZE OF HASH AFTER ADDING: is 5
- 示例 2:使用delete函数从哈希中删除元素
Perl
# Perl program to element from hash
#use warnings;
# creating hash
%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23);
# creating array of keys
@keys= keys %rateof;
# finding size of hash
$size = @keys;
print "SIZE OF HASH BEFORE DELETING: $size\n";
# using delete function
delete $rateof{'Mango'};
# creating array of keys
@keys= keys %rateof;
# finding size of hash
$size = @keys;
print "SIZE OF HASH AFTER DELETING: $size\n";
- 输出:
SIZE OF HASH BEFORE DELETING: 4
SIZE OF HASH AFTER DELETING: 3