Perl keys() 函数
Perl中的keys()函数以列表形式返回HASH中的所有键。列表中元素的顺序不需要总是相同,但是,它与value和每个函数返回的顺序相匹配。
语法: keys(HASH)
参数:
HASH:要打印其键值的哈希值。
返回: 对于标量上下文,它返回散列中的键数,而对于列表上下文,它返回一个键的列表。
例1 :
#!/usr/bin/perl
%hash = ('Ten' => 10,
'Eleven' => 11,
'Twelve' => 12,
'Thirteen' => 13);
@values = values( %hash );
print("Values are ", join("-", @values), "\n");
@keys = keys( %hash );
print("Keys are ", join("-", @keys), "\n");
输出
Values are 11-12-13-10
Keys are Eleven-Twelve-Thirteen-Ten
例2 :
#!/usr/bin/perl
%hash = ('Geek' => 1,
'For' => 2,
'Geeks' => 3);
@values = values( %hash );
print("Values are ", join("-", @values), "\n");
@keys = keys( %hash );
print("Keys are ", join("-", @keys), "\n");
输出
Values are 3-2-1
Keys are Geeks-For-Geek