I am having difficulty manipulating column based data using Perl in an AWK/Excel like way. There are some posts on stackoverflow that are similar that I am looking to do but not exactly. This is the closest example: http://stackoverflow.com/a/9179682/2379629 I am trying to sum and count multiple (many & variable) columns in Perl based on unique values in a "base" column. For example if this is the data set, I may want to use either the first column or second column as the "base" column. I then want to count & sum column's (fields) 3 & 4 for each value in the "Base" column. But I dont only want to do this for columns 3 & 4. I want the column numbers to be variable based on my input data set & my defined columns. here is an example:
__DATA__ U1 ID1 100 280 U1 ID1 137 250 U2 ID2 150 375 U1 ID2 100 100 U3 ID1 100 600 U9 ID3 137 200
example output: (Field[0]) U1 3 337 3 630 U2 1 150 1 375 U3 1 100 1 600 U9 1 137 1 200
or
example output: (Field[1]) ID1 3 300 3 1130 ID2 2 250 2 475 ID3 1 137 1 200
I could do that by modifying: $fields[0] to 0 or 1. But I cannot figure out how to dynamically set the number of columns. I believe I may need something like this below, where I can loop for each column. And chose the columns (fields) that I want to use in an @column = (1,2,10,20).
$res{$fields[0]}{$column}{instance}++; $res{$fields[0]}{$column}{volume} += $fields[$column];
I want to avoid the below, because then I need to write 2 lines of code for each column that I want to manipulate and it would make it harder to change the columns that I want.
$res{$fields[0]}{instance}++; $res{$fields[0]}{volume} += $fields[1]; $res{$fields[0]}{instance2}= (<insert code to count field[2]>) $res{$fields[0]}{volume2} += $fields[2];

In reply to Perl Sum & Count column based data by ler224

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.