珀尔 |有用的哈希函数
哈希是一组键值对。 Perl 存储散列的元素,以便根据其键搜索值。 Perl 提供了各种函数来对哈希执行操作,例如返回哈希值、从哈希中删除元素等。
例子:
#!/usr/bin/perl
# Initializing hash
%hash1 = ('Welcome' => 10, 'to' => 20, 'Geeks' => 80);
# To delete the List passed as parameter
$deleted_element = delete($hash1{'to'});
# Printing elements of Hash
print "$hash1{'Welcome'}\n";
print "$hash1{'to'}\n";
print "$hash1{'Geeks'}\n";
# Printing the deleted element
print "Deleted element: $deleted_element";
输出:
10
80
Deleted element: 20
下面列出了 Perl 中一些有用的哈希运算函数:
Function | Description |
---|---|
values() | Returns the list of all the values stored in a Hash |
keys() | Returns all the keys of the HASH as a list |
each() | Returns a Two-element list consisting of the key and value pair in List context and key for the next element when called in scalar context |
delete() | Used to delete the specified keys and their associated values from a hash, or the specified elements in the case of an array |