2014-03-31 64 views
0

我是新用Perl和我想哈希表的键生成使用XML::SIMPLE模块 这样打印散列键

$data = $xml->XMLin("tp.xml"); 

这里是结构产生

$VAR1 = { 
     'technical-profile' => { 
          'WEB' => { 
             'mandatory-param' => { 
                  'value' => 'high', 
                  'name' => 'screenCapability', 
                  'case-sensitive' => 'no' 
                 } 
            }, 
          'WAP/PDA' => { 
              'description' => 'wap/sparphone', 
              'mandatory-param' => { 
                   'value' => 'low|intermediate', 
                   'name' => 'screenCapability', 
                   'case-sensitive' => 'no' 
                  } 
             }, 
          'WAP' => { 
             'description' => 'wap/sparphone', 
             'mandatory-param' => { 
                  'value' => 'low', 
                  'name' => 'screenCapability', 
                  'case-sensitive' => 'no' 
                 } 
            } 
          } 
    }; 

我试过

print "Key: $_" foreach (keys%data); 

但我什么也没得到;我想打印WEBWAP/PDAWAP请问这是怎么回事?

回答

0

use strict;use warnings;包括在您制作的每个perl脚本的顶部。

如果你这样做了,你就已经得到了以下错误:

Global symbol "%data" requires explicit package name 

你的数据是分配给$data一个hashref。因此,要看到它的键,您执行以下操作:

print "Key: $_\n" for keys %$data; 

有关您的哈希的第二级的三个值,你会使用以下命令:

print "Key: $_\n" for keys %{$data->{technical-profile}}; 

应该输出(以随机顺序):

Key: WEB 
Key: WAP/PDA 
Key: WAP