in reply to Apache::ConfigFile iterate problem

Another hint: You may find the ref function useful. That, combined with moritz' suggested recursive function should do the trick.

...roboticus

Replies are listed 'Best First'.
Re^2: Apache::ConfigFile iterate problem
by tpais (Novice) on Sep 07, 2010 at 05:48 UTC

    Hi,

    I have tried that bevore.

    sub iterate_hash { my $ref = shift; foreach (keys %$ref) { if (ref($ref->{$_}) eq "HASH") { iterate_hash($ref->{$ +_}) } if (ref($ref->{$_}) eq "ARRAY" ) { iterate_array($ref- +>{$_})} else { print "$_ maps to $ref->{$_}\n" } } } sub iterate_array { my $ref = shift; foreach (@$ref) { if (ref($_) eq "HASH") { iterate_hash($_) } if (ref($_) eq "ARRAY" ) { iterate_array($_) } else { print "Array: $_\n" } } } &iterate_hash(\%vh);

    It didnt work out for me.

      It's a workable solution as indicated by others. So, if it didn't work for you you might ask where and why it failed, or tell us where if you know and ask why.

      I noticed something curious about your example code, and since you wrote it I'm wondering why you didn't find it curious. You're iterating over one level if it's an array but a different level if it's a hash. Is that really how your data structure looks?

      I'd suggest trying Data::Dumper to see what your data looks like at different points of your program. The processing code needs to process the proper part of the data each time, or you'll get odd results you didn't expect.

        Thank you for your help.

        There are indeed more levels with arrays.

        My code is working now. Thank you very much.

      tpais:

      If you provided a bit of test data and a description of what it did wrong, you'd have a pretty good question here.

      What's the difference between what this code does and what you want?

      ...roboticus