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

Hi everybody,

I am trying to create a hash of arrays where the arrays are empty at the beginning and will be filled later in a loop.

I create the hash with this code:
my %filehash = ("First Case"=>[], "Second Case"=>[], "Third Case"=>[], "Fourth Case"=>[]);
The loop where the arrays will be populated looks like this:
foreach my $file (@files) { if (<first case>) { push($filehash{"First Case"},$file); } <further checks> }
But now Eclipse (with EPIC) will not compile my code. The error message is:

"Type of arg 1 to push must be array (not hash element)"

What am I overlooking?
And, is this a Perl or an Eclipse problem?

Replies are listed 'Best First'.
Re: Problem with Populating a Hash of Arrays
by jwkrahn (Abbot) on Jan 09, 2009 at 07:41 UTC
    push($filehash{"First Case"},$file);

    You have to dereference the array to use it:

    push(@{$filehash{"First Case"}},$file);
      Cool. I knew it was something stupid like this. Thank you for the very fast answer.