in reply to Re^2: 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

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
  • Comment on Re^3: Setting up conditional check for printing out keys from array/hash ref
  • Download Code

Replies are listed 'Best First'.
Re^4: Setting up conditional check for printing out keys from array/hash ref
by JaeDre619 (Acolyte) on Oct 27, 2011 at 23:41 UTC
    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

        Thanks Grandfather. I did what you said and copied the dump output and added it to your code. It worked. What might be the problem is my config for using XML::Simple for reading the xml?

        Here is the setting for XML::Simple
        my $data = $xml->XMLin(`$lsCmd`, ForceArray => 1);
        Here is the additional hash output.
        my $lsResults3 = { 'recursive' => 'no', 'version' => '0.20.202.1.1101050227', 'time' => '2011-10-28T20:54:09+0000', 'filter' => '.*', 'path' => '/', 'directory' => [ { 'owner' => 'hdfs', 'group' => 'hdfs', 'permission' => 'drwxr-xr-x', 'path' => '/', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-06T19:30:33+0000' }, { 'owner' => 'xyz', 'group' => 'hdfs', 'permission' => 'drwx------', 'path' => '/A29', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-06T19:30:38+0000' }, { 'owner' => 'rawk', 'group' => 'hdfs', 'permission' => 'drwx------', 'path' => '/B29TABTV001', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-02-08T22:24:15+0000' }, { 'owner' => 'xyz', 'group' => 'hdfs', 'permission' => 'drwx------', 'path' => '/FOOBAR', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2010-10-04T10:44:17+0000' }, { 'owner' => 'xyz', 'group' => 'hdfs', 'permission' => 'drwxr-xr-x', 'path' => '/data', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-08-25T07:56:15+0000' }, { 'owner' => 'hdfs', 'group' => 'hdfs', 'permission' => 'drwxr-xr-x', 'path' => '/user', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-25T04:21:55+0000' } ], 'exclude' => '' }; for my $result ($lsResults1, $lsResults2, $lsResults3) { 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"; } } } }