Domanda

I am new with perl and i want to get keys of Hash table generated using XML::SIMPLE module like this

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

Here is the structure generated

$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'
                                                         }
                                    }
                           }
    };

I tried this

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

but i get nothing; i want to print the WEB WAP/PDA and WAP how is that possible please ?

È stato utile?

Soluzione

Include use strict; and use warnings; at the top of each and every perl script you make.

If you had done that you would've gotten the following error:

Global symbol "%data" requires explicit package name

Your data is a hashref assigned to $data. Therefore to see the keys of it, you do the following:

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

For the three values on the second level of your hash, you'd use the following:

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

Should output (in random order):

Key: WEB
Key: WAP/PDA
Key: WAP
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top