Sosi has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|