从嵌套哈希中查找值

时间:2014-10-24 09:29:35

标签: perl hash

我有一个哈希:

$VAR1 = {
          'x' => {
                          'Mathematics' => 82,
                          'Art' => 99,
                          'Literature' => 88
                        },
          'y' => {
                         'Mathematics' => 97,
                         'Literature' => 67
                       }
         .......
        };

我需要在数组中获取x, y等(目前我不知道x和y的值)

例如

@arr = (x,y, z..);

1 个答案:

答案 0 :(得分:3)

只需使用keys即可。如果哈希实际上是哈希引用,则首先取消引用它:

my @keys1 = keys %hash;
my @keys2 = keys %$hash_ref;