in reply to Forcing Array Context

Hi

The value of a hash key can only be a reference to an array, not an array itself. So: instead of:

# not $hierarchy{$directory} = (); # but $hierarchy{$directory} = []; # later, not push($hierarchy{$directory}, $filename); # but push(@{ $hierarchy{$directory} }, $filename);

perldoc perlref is your friend

HTH
ViceRaid

Replies are listed 'Best First'.
Re: Re: Forcing Array Context
by Anonymous Monk on Jun 25, 2003 at 09:46 UTC

    Thanks for the reply.

    If I were to then return \%hierarchy to a scalar variable how would I go about printing out the results?

      I suggest you take a look at the tutorials broquaint suggested; this should all become less mysterious. As for your question, the same principle applies as above: if you've got a reference in $ref put the sigil for the relevant type before it to dereference it. You'll get something like %$ref, @$ref, $$ref.

      my $hierarchy = get_hierarchy(@dirs); while ( my ( $directory, $contents ) = each %$hierarchy ) { ... # do your thing here }

      ViceRaid

      I think I've got it:

      for (keys %{ $scalar }) { print "$_\n" for @{$_}; }

      Does that look right?

        Yep, that's what you want. Sorry, I missed this post before posting below.