Hello monks! I would appreciate your enlightenment on the following. I have a tab-separated file as follows

A B n1 A B n2 A C n1 D E n2 D E n4 D F n1

and I want to count how many elements on the 3rd column does each element on the two other columns have. I.e. There are A=>3, A=>B=>2, A=>C=>1, etc. It is important for me to have the association between the first and second columns.

The easy way to solve this is just counting how many times A repeats, and how many times B repeats, etc. etc. But because I'm learning Perl, I'd like to know how to use a HoH to do this. In this regard, I built a HoH of the form

{ A => { B => [ n1, n2, ], C => [ n1, ] }, D => { E => [ n2, n4, ], F => [ n1, ] } }

I know how to do this for counting the number of values in the third level, and the number of values in the second level, but how can I multiply them? Ideally, what I'd like to do is to create a key "count" with the number of "n" in the third column for each. For instance

{ A => { count => 3, B => { count => 2, [ n1, n2, ], }, C => { count => 1, [ n1, ] } },etc.

Any tips on how to get this kind of value counting?


In reply to Count number of elements in HoH and sum them for other keys by Sosi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.