in reply to Walking Hash of hash issue?
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.
|
|---|