js1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I get the following error when running my program:

Bad index while coercing array into hash at ...

The code causing this problem is on the print line below due to the $file->{$t} variable. If I change this to $file{$t} the error goes away, but there's no value printed. However according to the Dumper below, there should be a value there. Could anyone tell me what I do to fix this please?

Thanks,

Ed.

foreach $file (@files) { print Dumper $file; foreach $t(keys %$file){ print "\nt= $t v=" . $file->{$t}; } ..
$VAR1 = [ { 'longname' => 'drwxr-xr-x 2 1ae2ets fq 4096 +Sep 14 2007 .', 'a' => bless( { 'uid' => 2004734, 'flags' => 15, 'mtime' => 1189777443, 'atime' => 1211362016, 'perm' => 16877, 'gid' => 2004409, 'size' => 4096 }, 'Net::SFTP::Foreign::Attributes' ), 'filename' => '.' }, { 'longname' => 'drwxr-xr-x 3 1ae2ets fq 4096 +Sep 14 2007 ..', 'a' => bless( { 'uid' => 2004734, 'flags' => 15, 'mtime' => 1189777162, 'atime' => 1211317201, 'perm' => 16877, 'gid' => 2004409, 'size' => 4096 }, 'Net::SFTP::Foreign::Attributes' ), 'filename' => '..' }, { 'longname' => '-r-------- 1 root root 0 +Jan 8 2007 .mirror', 'a' => bless( { 'uid' => 0, 'flags' => 15, 'mtime' => 1168287005, 'atime' => 1189777418, 'perm' => 33024, 'gid' => 0, 'size' => 0 }, 'Net::SFTP::Foreign::Attributes' ), 'filename' => '.mirror' } ];

Replies are listed 'Best First'.
Re: bad index while coercing array into hash
by BrowserUk (Patriarch) on May 21, 2008 at 10:26 UTC

    If     print Dumper $file; is producing:

    $VAR1 = [ { 'longname' => 'drwxr-xr-x 2 1ae2ets fq 4096 Sep 14 + 2007 .', ...

    Then you apparently have an AoAoH, not an AoH as you appear to think. So, in order to process the AoAoH, you would need to code something like:

    foreach $AoFiles (@files) { foreach $file ( @{ $AoFiles } ) { foreach $t ( keys %$file ){ print "\nt= $t v=" . $file->{$t}; } } ...

    Of course, depending how you are obtaining @files, it may well be that the outer loop above will only iterate once. The likely scenario is that you are doing something like:

    @files = someSubOrMethodThatReturnsAnAoHReference(); ...

    When you should be doing:

    $filesRef = someSubOrMethodThatReturnsAnAoHReference(); foreach $file ( @$filesRef ) { print Dumper $file; foreach $t(keys %$file){ print "\nt= $t v=" . $file->{$t}; } ...

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: bad index while coercing array into hash
by Erez (Priest) on May 21, 2008 at 10:35 UTC

    According to the Dump, $file is an AoH (Array of Hashes) calling foreach $t(keys %$file) tries to refer to the array as a hash. You need to say

    foreach $t(@{$file}) { foreach $tt(keys %{$t}){ print "\nt= $tt v=" . $t->{$tt}; } }

    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.