There are a large number of ways to solve this problem and the best solutions probably involve recursion to deal with nested directories. However, you haven't asked for such a solution (yet) so here's a solution that uses Perl 5.10 (and later)'s switch statement:

use strict; use warnings; use 5.010; my $lsResults1 = { 'recursive' => 'no', 'version' => '0.20.202.1.1101050227', 'time' => '2011-10-26T00:21:06+0000', 'filter' => '.*', 'path' => '/work/eng/feeds/customer_care', 'directory' => [ { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwxr-xr-x', 'path' => '/work/eng/feeds/customer_care', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-25T23:54:17+0000' }, { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwx------', 'path' => '/work/eng/feeds/customer_care/abc', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-25T17:12:56+0000' }, { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwx------', 'path' => '/work/eng/feeds/customer_care/def', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-25T21:05:50+0000' }, { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwx------', 'path' => '/work/eng/feeds/customer_care/test', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-25T21:28:14+0000' } ], 'exclude' => '' }; my $lsResults2 = { 'recursive' => 'no', 'version' => '0.20.202.1.1101050227', 'time' => '2011-10-26T00:30:02+0000', 'filter' => '.*', 'file' => { 'owner' => 'tst_act', 'replication' => '3', 'blocksize' => '134217728', 'permission' => '-rw-------', 'path' => '/work/eng/feeds/customer_care/test/q_data_20111 +023.dat', 'modified' => '2011-10-26T00:29:46+0000', 'size' => '379085', 'group' => 'eng', 'accesstime' => '2011-10-26T00:29:46+0000' }, 'path' => '/work/eng/feeds/customer_care/test', 'directory' => { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwx------', 'path' => '/work/eng/feeds/customer_care/test', 'accesstime' => '1970-01-01T00:00:00+0000', 'modified' => '2011-10-26T00:29:46+0000' }, 'exclude' => '' }; for my $result ($lsResults1, $lsResults2) { for (keys %$result) { when ('file') { print "Filename: $result->{file}{path}\n"; print " Size: $result->{file}{size}\n"; } when ('directory') { given (ref $result->{directory}) { when ('ARRAY') { for my $entry (@{$result->{directory}}) { print "Directory: $entry->{path}\n"; } } when ('HASH') { print "Directory: $result->{directory}{path}\n"; } } } } }

Prints:

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 Filename: /work/eng/feeds/customer_care/test/q_data_20111023.dat Size: 379085 Directory: /work/eng/feeds/customer_care/test

Notice the use of ref to determine what type of directory entry is being dealt with and the use of keys to work through the hash entries looking or the interesting stuff.

True laziness is hard work

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.