JaeDre619 has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I am trying to learn how to dereference hashref/arrayrefs. So far I have managed to get my test results I want, but having to write (2) different scripts for it.
They are basically the same except for the iteration of the values. I need some help constructing a condition to print the keys I need.
I have a couple of data structures (see below) which is from an xml file and is handled using XML::Simple using ForceArray =>1.
Here is the data structure from dumper, I am working with:
$VAR1 = { '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_car +e', 'accesstime' => '1970-01-01T00:00:00+00 +00', 'modified' => '2011-10-25T23:54:17+0000 +' }, { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwx------', 'path' => '/work/eng/feeds/customer_car +e/abc', 'accesstime' => '1970-01-01T00:00:00+00 +00', 'modified' => '2011-10-25T17:12:56+0000 +' }, { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwx------', 'path' => '/work/eng/feeds/customer_car +e/def', 'accesstime' => '1970-01-01T00:00:00+00 +00', 'modified' => '2011-10-25T21:05:50+0000 +' }, { 'owner' => 'tst_act', 'group' => 'eng', 'permission' => 'drwx------', 'path' => '/work/eng/feeds/customer_car +e/test', 'accesstime' => '1970-01-01T00:00:00+00 +00', 'modified' => '2011-10-25T21:28:14+0000 +' } ], 'exclude' => '' };
Here is the code part to extract the directories:
my $lsResults = $cmd->ls("$chkdir"); #$chkdir = "/work/eng/feeds/custo +mer_care" #print Dumper($lsResults); for my $dirnm ( @{$lsResults->{directory}} ) { print "Directory: " . $dirnm->{path} . "\n"; }
Results:
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
The results are fine, until I list the sub-directory that happens to have files: $chdkir="/work/eng/feeds/customer_care/test"
Which in the case the test directory has files and size information that I want to list, but can't due to this message:
Not an ARRAY reference at ./tst.plHere is the data dump with that new directory, that shows a file:
$VAR1 = { '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_20111023.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' => '' };
So, to get the file and sizes, I am using this in a separate perl script
my $lsResults = $cmd->ls("$chkdir"); #print Dumper($lsResults); for my $filenm ( @{$lsResults->{file}} ) { print "Filename: " . $filenm->{path} . "\n"; print " Size: " . $filenm->{size} . "\n"; }
Results:
Filename: /work/eng/feeds/customer_care/test/q_data_20111023.dat Size: 379085
What I need help is combining these scripts, combining the logic where: whatever directory I pass in, if the directory has sub-directories, list the sub-directory. If the directory has filenames, list the directory, file, and size.
So, in this case if $chkdir="/work/eng/feeds/customer_care/test"
Expected Output:
Directory: /work/eng/feeds/customer_care/test Filename: /work/eng/feeds/customer_care/test/q_data_20111023.dat Size: 379085
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Setting up conditional check for printing out keys from array/hash ref
by GrandFather (Saint) on Oct 27, 2011 at 08:15 UTC | |
by JaeDre619 (Acolyte) on Oct 27, 2011 at 13:26 UTC | |
by GrandFather (Saint) on Oct 27, 2011 at 22:39 UTC | |
by JaeDre619 (Acolyte) on Oct 27, 2011 at 23:41 UTC | |
by GrandFather (Saint) on Oct 28, 2011 at 07:30 UTC | |
| |
|
Re: Setting up conditional check for printing out keys from array/hash ref
by choroba (Cardinal) on Oct 27, 2011 at 07:51 UTC |