The problem is not in Perl, but in your use of variables. Once you allow Perl to tell you what's wrong, it becomes more obvious - add use strict; to your program.

The whole situation is not helped by an experimental feature of Perl that allowed (hash) references to be used like hashes. This feature is still experimental but I think it's on its way out of Perl again.

You're reading your XML into $professionsXML but then you're accessing %professionsXML.

The following script goes through your data structure and whenever it can go deeper in the structure, it does so. See also ref.

#!perl use strict; use XML::Simple; use Data::Dumper; my $xmlSimple = new XML::Simple(KeepRoot => 1); my $xml = join "", <DATA>; my $professionsXML = $xmlSimple->XMLin($xml); print Dumper($professionsXML); print qq(0.1: $professionsXML->{'profs'}->{'name'}\n); print qq(0.2: $professionsXML->{'profs'}->{'profcats'}->{'name'}\n); foreach my $key (keys %{$professionsXML}) { print qq(1: $key\n); foreach my $key2 (keys %{$professionsXML->{$key}}) { print qq(2: $key2\n); if( ref $professionsXML->{$key}->{$key2} ) { foreach my $key3 (keys %{$professionsXML->{$key}->{$key2}} +) { print qq(3: $key3\n); if( ref $professionsXML->{$key}->{$key2}->{$key3}) { foreach my $key4 (keys %{$professionsXML->{$key}-> +{$key2}->{$key3}}) { print qq(4: $key4\n); } }; } }; } } exit(0); __DATA__ <profs> <name>prof</name> <profcats> <name>cat</name> <profcatgroups> <name>group</name> <profcatgroup> <name>prof1</name> </profcatgroup> </profcatgroups> </profcats> </profs>

Please consider using something other than XML::Simple - it will cause you no end of headache in the long run. As soon as the structure of your XML is not easily guessable (for example, repeated elements in profcats, your code will break.


In reply to Re: Walking Hash of hash issue? by Corion
in thread Walking Hash of hash issue? by Syrkres

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.