in reply to Re: Setting up conditional check for printing out keys from array/hash ref
in thread Setting up conditional check for printing out keys from array/hash ref

thanks for your input. yes, the solution I was looking for was to make it recursive. unfortunately, the system I am using has perl version v5.8.6. What changes would I have to make with your solution?
  • Comment on Re^2: Setting up conditional check for printing out keys from array/hash ref

Replies are listed 'Best First'.
Re^3: Setting up conditional check for printing out keys from array/hash ref
by GrandFather (Saint) on Oct 27, 2011 at 22:39 UTC

    The sample solution I offered is not in itself recursive, although it could be the guts of a recursive solution.

    given/when is Perl's switch statement support which was introduced with 5.10. A switch statement is really just syntactic sugar for a set of related if statements so you can replace the when blocks with if blocks:

    for my $result ($lsResults1, $lsResults2) { for (keys %$result) { if ($_ eq 'file') { print "Filename: $result->{file}{path}\n"; print " Size: $result->{file}{size}\n"; } elsif ($_ eq 'directory') { my $value = ref $result->{directory}; if ($value eq 'ARRAY') { for my $entry (@{$result->{directory}}) { print "Directory: $entry->{path}\n"; } } elsif ($value eq 'HASH') { print "Directory: $result->{directory}{path}\n"; } } } }
    True laziness is hard work
      Thanks Grandfather! It works nicely. I am doing some testing on it right now.

      There's a point in the directory (where I go higher up in directory) where I see this messageBad index while coercing array into hash

      What's a good way to handle that message?

      Output:

      $ ./tst-loopthrough2.pl --dir /work/eng Bad index while coercing array into hash at ./tst-loopthrough2.pl line + 101. $ ./tst-loopthrough2.pl --dir /work/eng/feeds Directory: /work/eng/feeds Directory: /work/eng/feeds/customer_care $ ./tst-loopthrough2.pl --dir /work/eng/feeds/customer_care Directory: /work/eng/feeds/customer_care Directory: /work/eng/feeds/customer_care/abc Directory: /work/eng/feeds/customer_care/def Directory: /work/eng/feeds/customer_care/test $ ./tst-loopthrough2.pl --dir /work/eng/feeds/customer_care/test Filename: /work/eng/feeds/customer_care/test/q_data_20111023.dat Size: 379085 Directory: /work/eng/feeds/customer_care/test

        The sample code I provided is fewer than 100 lines including the sample hash initialisation. That is less than the line containing the error according to your error message so I can't easily tell where the error is happening. Perhaps you'd like to modify the data in my sample code to reproduce the error then post that?

        True laziness is hard work