Perl by Example - 02

5.5. Hash (Associative Array) Functions

5.5.1. The keys Function

The keys function returns, in random order, an array whose elements are the keys of a hash (see "The values Function" and "The each Function").

Format

keys(ASSOC_ARRAY)
keys ASSOC_ARRAY

Example 5.53.

(In Script)
    # The keys function returns the keys of a hash
1   %weekday= (
               '1'=>'Monday',
               '2'=>'Tuesday',
               '3'=>'Wednesday',
               '4'=>'Thursday',
               '5'=>'Friday',
               '6'=>'Saturday',
               '7'=>'Sunday',
              );
2   foreach $key ( keys(%weekday) ){print "$key ";}
    print "\n";
3   foreach $key ( sort keys(%weekday) ){print $key ;}
    print "\n";

(Output)
2   7 1 2 3 4 5 6
3   1 2 3 4 5 6 7

Explanation

  1. The hash %weekday is assigned keys and values.

  2. For each value in %weekday, call the keys function to get the key. Assign the key value to the scalar $key and print it in random order.

  3. Now the keys are sorted and printed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值