Perl 简明教程
Perl - Hashes
哈希是一组 key/value 对。哈希变量前面带有百分号 (%) 符号。要引用哈希的单个元素,您将使用哈希变量名称,其前面带有“$”符号,后面用花括号括起与值关联的“键”。
A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets..
以下是一个使用哈希变量的简单示例 −
Here is a simple example of using the hash variables −
#!/usr/bin/perl
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
print "\$data{'John Paul'} = $data{'John Paul'}\n";
print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";
这会产生以下结果 −
This will produce the following result −
$data{'John Paul'} = 45
$data{'Lisa'} = 30
$data{'Kumar'} = 40
Creating Hashes
哈希使用以下两种方式之一创建。在第一种方法中,您逐个为命名键分配一个值 −
Hashes are created in one of the two following ways. In the first method, you assign a value to a named key on a one-by-one basis −
$data{'John Paul'} = 45;
$data{'Lisa'} = 30;
$data{'Kumar'} = 40;
在第二种情况下,您使用列表,该列表通过从列表中获取各个对来转换:此对中的第一个元素用作键,第二个元素用作值。例如 −
In the second case, you use a list, which is converted by taking individual pairs from the list: the first element of the pair is used as the key, and the second, as the value. For example −
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
为清楚起见,可以使用 ⇒ 作为“,”的别名,以指示键/值对,如下所示 −
For clarity, you can use ⇒ as an alias for , to indicate the key/value pairs as follows −
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
以下是上述形式的另一个变体,请看一看,此处所有键都已加上了连字符 (-),并且它们的周围不需要引号 −
Here is one more variant of the above form, have a look at it, here all the keys have been preceded by hyphen (-) and no quotation is required around them −
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
但要注意的是,此种形式的哈希形成中使用了一个没有空格的单词,即键,并且如果您以这种方式建立哈希,则只能使用连字符来访问键,如下所示。
But it is important to note that there is a single word, i.e., without spaces keys have been used in this form of hash formation and if you build-up your hash this way then keys will be accessed using hyphen only as shown below.
$val = %data{-JohnPaul}
$val = %data{-Lisa}
Accessing Hash Elements
当从哈希中访问各个元素时,您必须在变量前加上美元符号 ($),然后在变量名称后面用花括号括起元素键。例如 −
When accessing individual elements from a hash, you must prefix the variable with a dollar sign ($) and then append the element key within curly brackets after the name of the variable. For example −
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
print "$data{'John Paul'}\n";
print "$data{'Lisa'}\n";
print "$data{'Kumar'}\n";
这会产生以下结果 −
This will produce the following result −
45
30
40
Extracting Slices
您可以提取哈希切片,就像提取数组切片一样。您需要为变量使用 @ 前缀来存储返回的值,因为它们将成为值列表 −
You can extract slices of a hash just as you can extract slices from an array. You will need to use @ prefix for the variable to store the returned value because they will be a list of values −
#!/uer/bin/perl
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
@array = @data{-JohnPaul, -Lisa};
print "Array : @array\n";
这会产生以下结果 −
This will produce the following result −
Array : 45 30
Extracting Keys and Values
可以用 keys 函数获取散列中所有键的一个列表,该函数具有以下语法:
You can get a list of all of the keys from a hash by using keys function, which has the following syntax −
keys %HASH
此函数返回命名散列中所有键的数组。以下是示例:
This function returns an array of all the keys of the named hash. Following is the example −
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@names = keys %data;
print "$names[0]\n";
print "$names[1]\n";
print "$names[2]\n";
这会产生以下结果 −
This will produce the following result −
Lisa
John Paul
Kumar
同样,可以使用 values 函数来获取所有值的一个列表。此函数具有以下语法:
Similarly, you can use values function to get a list of all the values. This function has the following syntax −
values %HASH
此函数返回由命名散列中的所有值组成的普通数组。以下是示例:
This function returns a normal array consisting of all the values of the named hash. Following is the example −
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@ages = values %data;
print "$ages[0]\n";
print "$ages[1]\n";
print "$ages[2]\n";
这会产生以下结果 −
This will produce the following result −
30
45
40
Checking for Existence
如果你尝试从不存在的散列中访问一个键/值对,通常会获得 undefined 值,并且如果你禁用了警告,则会在运行时收到一个警告。你可以通过使用 exists 函数避免这种情况,无论它的值是什么,该函数在命名键存在时返回 true:
If you try to access a key/value pair from a hash that doesn’t exist, you’ll normally get the undefined value, and if you have warnings switched on, then you’ll get a warning generated at run time. You can get around this by using the exists function, which returns true if the named key exists, irrespective of what its value might be −
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
if( exists($data{'Lisa'} ) ) {
print "Lisa is $data{'Lisa'} years old\n";
} else {
print "I don't know age of Lisa\n";
}
这里我们引入了 IF…ELSE 语句,我们将在单独的章节中学习该语句。现在只需假设仅当给定条件为真时执行 if( condition ) 部分,否则执行 else 部分。因此,当我们执行上述程序时,它会产生以下结果,因为这里给定的条件存在($data{'Lisa'} 返回 true):
Here we have introduced the IF…ELSE statement, which we will study in a separate chapter. For now you just assume that if( condition ) part will be executed only when the given condition is true otherwise else part will be executed. So when we execute the above program, it produces the following result because here the given condition exists($data{'Lisa'} returns true −
Lisa is 30 years old
Getting Hash Size
你可以通过对键或值使用标量上下文来获取大小,即散列中的元素数。简单来说,你需要先获取键或值的数组,然后可以按照以下方式获取数组的大小:
You can get the size - that is, the number of elements from a hash by using the scalar context on either keys or values. Simply saying first you have to get an array of either the keys or values and then you can get the size of array as follows −
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size: is $size\n";
@values = values %data;
$size = @values;
print "2 - Hash size: is $size\n";
这会产生以下结果 −
This will produce the following result −
1 - Hash size: is 3
2 - Hash size: is 3
Add and Remove Elements in Hashes
使用简单的赋值运算符,可以用一行代码添加一个新的键/值对。但是,若要从散列中删除一个元素,则需要使用 delete 函数,如下例所示:
Adding a new key/value pair can be done with one line of code using simple assignment operator. But to remove an element from the hash you need to use delete function as shown below in the example −
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size: is $size\n";
# adding an element to the hash;
$data{'Ali'} = 55;
@keys = keys %data;
$size = @keys;
print "2 - Hash size: is $size\n";
# delete the same element from the hash;
delete $data{'Ali'};
@keys = keys %data;
$size = @keys;
print "3 - Hash size: is $size\n";
这会产生以下结果 −
This will produce the following result −
1 - Hash size: is 3
2 - Hash size: is 4
3 - Hash size: is 3