珀尔 |每个()函数
当在 List 上下文中调用时,此函数返回一个由哈希的下一个元素的键和值对组成的双元素列表,以便您可以对其进行迭代。而在标量上下文中调用时,它仅返回哈希的下一个元素的键。
Syntax: each MY_HASH
Parameter:
MY_HASH is passed as a parameter to this function
Returns:
A 2-element list of key-value pairs for the List context whereas only the key for the scalar context.
示例 1:
#!/usr/bin/perl
# Initializing a Hash
%hash = (Geeks => 1, of => 2 , Geek => 3);
# each() function
while (($key, $value) = each(%hash))
{
# Printing(key, value) pair
print("$key = $value\n");
}
输出:
Geek = 3
of = 2
Geeks = 1
示例 2:
#!/usr/bin/perl
# Initializing a Hash
%hash = (Geeks, of, Geek);
# each() function for scalar context
while (($key) = each(%hash))
{
# Printing(key)
print("$key ");
}
输出:
Geek Geeks