Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Push to an array in a hash of hashes

by Endless (Beadle)
on Aug 07, 2013 at 21:34 UTC ( [id://1048438]=perlquestion: print w/replies, xml ) Need Help??

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

So, yeah, I'm confusing myself. But here's the situation: Scanning for terms through multiple files, I want to keep track of each place a term is found in addition to the number of times the term shows up. So, I have hash of hashes with a scalar $count and an array $location. At least, that's what I want to have. So, how can I make this work? Yes, before you ask, at this point I am just throwing around @{$} and whatever other symbols I can come up with. Teach me, oh wise ones!

my %hoh; my @arr = ('first', 'second', 'third'); push (@{$hoh{'word'}}{'array'}, @arr); #no good $hoh{'word'}{'count'} += 1; $hoh{'word'}{'count'} += 1; say "The count of 'word' is ", $hoh{'word'}{'count'}; say "The array of 'word' is ", @{$hoh{'word'}}{'array'}; # still no go +od.

Replies are listed 'Best First'.
Re: Push to an array in a hash of hashes
by kennethk (Abbot) on Aug 07, 2013 at 21:44 UTC
    You missed with your curlies; you are going to store an array reference in $hoh{'word'}{'array'}, and then dereference that, so curlies go around the whole expression.
    use strict; use warnings; use 5.10.0; my %hoh; my @arr = ('first', 'second', 'third'); push (@{$hoh{'word'}{'array'}}, @arr); #now good $hoh{'word'}{'count'} += 1; $hoh{'word'}{'count'} += 1; say "The count of 'word' is ", $hoh{'word'}{'count'}; say "The array of 'word' is @{$hoh{'word'}{'array'}}"; # now good.

    See perlreftut and/or perllol. Note also I moved your array into the double quotes, so it interpolates w/ spaces (by default).


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Note: The pertinent default is the value of the special variable LIST_SEPARATOR ($").
      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1048438]
Approved by frozenwithjoy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-07-14 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.