0

Code below is part of XML structure which i get from XML::Simple.

I get many classfield (classfield_101, classfield_1023, ... ) and classfield_101 is one of them. For classfield_101 I expect the program to print 'CHECK UNCHECK' to the console.

my (%ClassDetails) = ();
my %struct = ( 
    'level1' => {
        'classfield' => {
            'classfield_101' => {
                'classfieldvalues' => { 
                    'classfieldvalue' => [ 
                        {'content' => 'CHECK', 'key' => '1254789'}, 
                        {'content' => 'UNCHECK', 'key' => '1987456'}
                    ]   
                },  
                'classfieldname' => 'CHECKCLASS',
                'key' => 'CHECKDETAILS'
            },  
        }   
    },  
);

print Dumper(\%struct);

for my $elem (@{$struct{level1}{classfield}{classfield_101}{classfieldvalues}{classfieldvalue}}) {
    print "$elem->{content} ";
}



#Output
#CHECK UNCHECK

How to loop over each classfield_id in %$classfield and print values ?

foreach $classfield_id (keys %$classfield) {
    ## for ex: Looping for classfield_id = classfield_101 from %$classfield
    if ( $classfield->{$classfield_id}->classfieldname eq "CHECKCLASS" ) {

       ### Stuck here --- $ClassDetails{checkuncheck} = How to print 

    }
}
zdim
  • 64,580
  • 5
  • 52
  • 81
  • 2
    Different code is needed if there are 0, 1 or 2+ `classfieldvalue`. Don't use [such a complicated module](https://stackoverflow.com/questions/33267765/why-is-xmlsimple-discouraged)! – ikegami Jun 26 '18 at 19:44

1 Answers1

-1

The below will print your whole hash. You can modify the code to print whatever you are interested in:

foreach my $key (keys %struct) {
    print "$key\n";
    foreach my $key2 (keys %{$struct{$key}}) {
        print "-$key2\n";
        foreach my $key3 (keys %{$struct{$key}->{$key2}}) {
            print "--$key3\n";
            foreach my $key4 (keys %{$struct{$key}->{$key2}->{$key3}}) {
                my $ref = ref $struct{$key}->{$key2}->{$key3}->{$key4};
                if($ref eq '') {
                    print "---$key4 = $struct{$key}->{$key2}->{$key3}->{$key4}\n";
                }
                else {
                    print "---$key4\n";
                    foreach my $key5 (keys %{$struct{$key}->{$key2}->{$key3}->{$key4}}) {
                        print "----$key5\n";
                        foreach my $key6 (@{$struct{$key}->{$key2}->{$key3}->{$key4}->{$key5}}) {
                            foreach my $key7 (keys %{$key6}) {
                                print "-----$key7 = $key6->{$key7}\n";
                            }
                        }
                    }
                }
            }
        }
    }
}
Andrey
  • 1,808
  • 1
  • 16
  • 28
  • 2
    Naming all your variables `key` makes this code totally unreadable. /// Why do you think there could be more than one `classfield` and `classfieldvalues` element? /// Your code fails to handle docs with a single classfield or a single classfieldvalue. The data structure produced by XML::Simple [varies greatly](https://stackoverflow.com/a/33273488/589924) based on element counts and such. – ikegami Jun 26 '18 at 19:44