angerusso:

I'd suggest building an array to map column number to the column name, which it looks like you may be doing. I'd also suggest using a hash to act as your accumulator: the key would be the column name (G1, G2, G3).

Then the procedure would be something like this:

So lets see how it would work with this input data:

Gname G1 G1 G2 G3 G3 A W M M W W A M W W M M B M W M M M

So we first create a hash to hold your counters, starting like { }

So we read the first line, and find it's a header, so we build a column number to name map, resulting in the map: 1-->G1, 2-->G1, 3-->G2, 4-->G3, 5-->G3, then go to the next line.

For the second line, we split it into columns.

The first column (column 0) will be the name of the counter in our hash. So let's scan through the columns for interesting values: Column 1 has 'W' so it's boring and we skip over it. Column 2 has an 'M', so we want to count it. So we consult our column to name table, and find that column 2 maps to G1. So we update the counter slot for Gname=A, Colheader G1 to 1. Column 3 also has a 'M', and column 3 maps to G2, so update counter for A/G2 to 1. The remaining columns are 'W', so we ignore them.

We split the next line up and find that it's the same Gname (A). Working through the columns, we see that the interesting columns are 1, 4 and 5, which map to G1, G3 and G3. So the A/G1 counter increases to 2, the A/G2 counter increases to 2 also. The A/G3 counter is set to 1.

The final line gives us a new Gname, B. It's interesting columns are 1, 3, 4 and 5, which map to G1, G2, G3 and G3 respectively. So we set both B/G1 and B/G2 to 1, and B/G3 to 2.

So our final counter hash shows that for those three rows, we would have:

Gname G1 G2 G3 A 2 2 1 B 1 1 2

Update: Added the "Create a hash" line to show where it should be created, and then the worked out example.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: how to sum over rows based on column headings in perl by roboticus
in thread how to sum over rows based on column headings in perl by angerusso

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.