Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Accessing deeply burried arrays of hashes

by Mandor (Pilgrim)
on Jan 25, 2001 at 03:50 UTC ( #54155=perlquestion: print w/replies, xml ) Need Help??

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

Hi, This is somehow a follow up to "Getting keys of a hash element"
Okay, what I want to do is something like this :
$tophash{$value1}{$value2} = [$a, $b, $c, $d]; foreach $key (sort keys %tophash) { $value1 = $key; print "$value1"; foreach $subkey (sort keys %{$tophash{$value1}}) { $value2 = $subkey; foreach $subsubkey (sort %{$tophash{$value}{$value2}} ) { print "$subsubkey"; } } }
The problem with this is that perl puts out this :

(F) You used an array where a hash was expected, but the array has no information on how to map from keys to array indices.
You can do that only with arrays that have a hash reference at index 0.

What I want is to be able to access this key and get just a plain array with normal position markers (0 = $a, 1 = $b, etc.) back.
I am glad about any help. Thanks.


Replies are listed 'Best First'.
Re (tilly) 1: Accessing deeply burried arrays of hashes
by tilly (Archbishop) on Jan 25, 2001 at 04:12 UTC
    Suggestion. Dereference one at a time as you enter the loop. The logic will be simpler, the syntax saner, and by avoiding nested hash lookups your code will be faster as well:
    foreach my $key (sort keys %tophash) { my $value1 = $tophash{$key}; foreach my $subkey (sort keys %$value1) { my $value2 = $value1->{$subkey}; foreach my $val (sort @$value2 ) { print "$val\n"; } } }
    Oops, forgot to do the second hash lookup. Fixed.
Re: Accessing deeply burried arrays of hashes
by arturo (Vicar) on Jan 25, 2001 at 03:58 UTC

    The problem seems to be that you're treating your anonymous array as a hash in this bit of code:

    foreach $subsubkey (sort %{$tophash{$value}{$value2}} ) { print "$subsubkey"; }

    It's an array, so treat it as one. This should get you started:

    foreach $array_value ( @$tophash{$value}{$value2} ) { print $array_value; }

    Notice you don't *need* the extra set of braces (but leave 'em in if they make things clearer to you.

    Update This last sentence is false, as merlyn notes in his response. So see that one =)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Uh, you're missing a curly set:
      Without that, it parses like:
      $tophash must be a hashref, so deref it as a slice with $value being the (only) key, then take that slice (ugh) and deref it (them?) as a hashref with $value2 for the key.
      Definitely perverted. Almost certainly wrong.

      -- Randal L. Schwartz, Perl hacker

Re: Accessing deeply burried arrays of hashes
by Trinary (Pilgrim) on Jan 25, 2001 at 04:02 UTC
    $tophash{$value1}{$value2} = [$a, $b, $c, $d];
    change this to:
    $tophash->{$value1}{$value2} = [$a,$b,$c,$d];

    This can be referenced with $tophash->{$value1}{$value2}[$index], however you'd like. Each value in $tophash->{$one}{$two} will be a reference to an array, in your print statement something like print $subsubkey->[0] would get you at $a



      It makes very little difference here whether the top structure is a hash or a scalar containing a hashref. That's not where the problem was.

      -- Randal L. Schwartz, Perl hacker

      Thank you for all your answers, folks. They have helped me greatly.
Re: Accessing deeply burried arrays of hashes
by ryddler (Monk) on Jan 25, 2001 at 04:11 UTC
    In addition to what everyone else commented on above, you also spelled your scalar variable $value1 in the beginning of the snippet:
    $tophash{$value1}{$value2} = ...
    and $value later on in the loop where you are trying to reference it:
    foreach $subsubkey (sort @{$tophash{$value}{$value2}} )

      Which the handy little spellchecker would have caught... :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://54155]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2020-01-21 17:29 GMT
Find Nodes?
    Voting Booth?