http://www.perlmonks.org?node_id=939597

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

Hello,

I'm tring to add a list to a fairly complicated hash of hashes data structure.

$REPO_HASH {$gqueried_repo} {REPO_BRANCHES} = @branch_list

@branch_list is a simple array that does contain valid info which can be displayed in a for loop.

When I try to print the the above hash I only the length of the array like:

Testing branch display=1

Using:

foreach my $k ($REPO_HASH {$gqueried_repo} {REPO_BRANCHES}) { print "Testing branch display=$k\n"; }

What I'm going with the Hash seems to be ok where similar to the following works fine for scalars:

print RPTR "$REPO_HASH{$repository}{REPO_OWNER}. ";

I'm good with perl, but this is a little above me. Can anyone just suggest what I'm doing wrong plz...

Replies are listed 'Best First'.
Re: Howto load list context into hash of hashes?
by NetWallah (Canon) on Nov 23, 2011 at 05:58 UTC
    $REPO_HASH {$gqueried_repo} {REPO_BRANCHES} = @branch_list ; # Does not do what you apparently think it does # The result of the assignment above is the number of elements in @b +ranch_list
    What I think you meant:
    $REPO_HASH {$gqueried_repo} {REPO_BRANCHES} = \@branch_list; #Note: Added "\", which makes it a REF to the array
    You can unwind the HOHOA using:
    for my $k (@{ $REPO_HASH {$gqueried_repo} {REPO_BRANCHES} }){ # Process one item of the array in $k }

                "XML is like violence: if it doesn't solve your problem, use more."

      You hit it right the head! I'd already tried the "\@branch_list" but I couldn't get get the "unwind" part down. I can finally move forward without having to redesign the script. Thanks a million!!

Re: Howto load list context into hash of hashes?
by TJPride (Pilgrim) on Nov 23, 2011 at 11:10 UTC
    Two minor notes unrelated to the solution already posted.

    1) In Perl, for is the same as foreach - no need to type extra characters unless you're used to PHP and just prefer foreach.

    2) I'm assuming REPO_BRANCHES is hard-coded value, but since you didn't put it in quotes, there's no way to tell if it's hard-coded or a declared constant. If the former, you should probably put it in quotes.

      Responding to your second point:
      "REPO_BRANCHES" is NOT a hard-coded value. It is a "simple identifier", and would normally be quoted.

      However (see perldata), inside curlies, perl will auto-quote it, so you do not need to. Most programmers do not use quotes when accessing hash values, because it is not necessary and adds keystrokes without adding value.

                  "XML is like violence: if it doesn't solve your problem, use more."

        By "hard-coded" I mean a manually-entered value as opposed to a variable. What you probably mean by "simple identifier". And yes, Perl will auto-quote it, but my point is that in this particular context it can be ambiguous to someone reading the code, so in my opinion it's not good practice to leave the quotes out. We can agree to disagree on that one.