Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Problem with counting the nodes in a hash created by XML::Simple from a XML file.

by turbodizik (Initiate)
on Jul 18, 2013 at 12:38 UTC ( #1045086=perlquestion: print w/replies, xml ) Need Help??

turbodizik has asked for the wisdom of the Perl Monks concerning the following question:

Dear community, I have a problem when I try to count the items in a hash variable which was created by XML::Simple from a xml file.

My xml looks like:
<?xml version="1.0" encoding="UTF-8"?> <config> <log> <destination>e1@mail.de</destination> <destination>e2@mail.de</destination> <destination>e3@mail.de</destination> </log> <item> <id>0</id> <filename>file1</filename> <extension>txt</extension> </item> <item> <id>1</id> <filename>file2</filename> <extension>txt</extension> </item> </config>

if i try to count the <destination> items with scalar (keys $config->{log}->{destination}); AND there are more then 1 destinations, then it works fine. But if I reduce it in the xml to just one email address it throws an error:
Type of argument to keys on reference must be unblessed hashref or arr +ayref at test.pl line 13.
___________________

The second problem which I have is to count the <item> nodes. If there are more then one nodes it works fine, like by <destination>. But if there is just one <item> node, it counts the nodes in this <item>. In my case it is 3 (<id>,<filename>,<extension>).

The way I translated my xml file in Hash is:
use XML::Simple qw(:strict); $config = XMLin('config.xml', KeyAttr => { server => 'name' }, ForceAr +ray => [ 'server', 'address' ]);

I would be glad, if someone could help me, because I didn't find anything while googling...

thanks a lot!
Dimitri

p.s
by some reason the word array splits here automatically in "ar+
ray"

Replies are listed 'Best First'.
Re: Problem with counting the nodes in a hash created by XML::Simple from a XML file.
by Corion (Pope) on Jul 18, 2013 at 12:45 UTC

    You are already using ForceArray. Maybe you want to add destination to that list?

      ... and item as well...

      It was the solution! Thank you very much! I'm just silly, cause I forgot about this possibility, to force an array. :)
Re: Problem with counting the nodes in a hash created by XML::Simple from a XML file.
by Lotus1 (Vicar) on Jul 18, 2013 at 13:09 UTC

    I find Data::Dumper is helpful for understanding what is actually there in data structures I create. It would show you what you are getting from XML::Simple.

Re: Problem with counting the nodes in a hash created by XML::Simple (use XML::Rules, Data::Diver )
by Anonymous Monk on Jul 18, 2013 at 13:22 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045086]
Approved by hdb
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-03-02 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (35 votes). Check out past polls.

    Notices?