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

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

    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"; } } } }

        I'd avoid XML::Simple. Use XML::Twig instead. XML::Twig looks more complicated (and is), but it's often easier to get right! The problem is that XML::Simple makes guesses about sensible defaults for handling XML files that are fine for a small number of simple applications such as configuration files (where it works very well), but are just plain broken for many tasks. And when stuff breaks with XML::Simple it's often hard to figure out why. Note that it is XML::Simple, not XML::Easy!

        True laziness is hard work