Anas:

OK, so you have some columns in your report that you're using to create groups, and some columns to collect data for. You don't specify everything, so I'll guess:

my @columns_to_group_on = (2, 3); my @columns_to_average = (5, 6);

Then, as you read the file, you'll want to split the data into columns. Then from those columns, you'll want to figure out what the group key will be, and the data you're capturing:

my $group = join("/", @columns[@columns_to_group_on]); my @row = @columns[@columns_to_average];

And then update your data for the group:

++$data{$group}{ROWCNT}; $data{$group}{SUMS}[$_] += $row[$_] for 0 .. $#row;

Once you've collected all the data from the file, you need only build the report, so you compute your averages from the data you've collected, and print it:

my @avg = [ map { $_/$data{$group}{ROWCNT} } @{$data{$group}{SUMS}} ]; print "GROUP $group: [", join(", ", @avg), "]\n";

Add in your loops and control logic to get something like this:

$ perl pm_121222.pl Collecting the data into groups: 1: xxxx A 1 G L -135.185 178.150 179.885 0.000 0.000 0.000 1 0.000 P 1 + F 1 2: xxxx A 2 S L -49.668 -31.158 177.985 0.000 0.000 0.000 1 0.000 P 1 +F 1 3: xxxx A 3 M L -84.632 -45.215 177.518 0.000 0.000 0.000 1 0.000 P 1 +F 1 4: xxxx A 1 G L -115.240 -69.349 -171.360 0.000 0.000 0.000 1 0.000 P +1 F 2 5: xxxx A 2 S L -84.776 -143.809 173.303 0.000 0.000 0.000 1 0.000 P 1 + F 2 6: xxxx A 3 M L -50.674 158.544 177.747 0.000 0.000 0.000 1 0.000 P 1 +F 2 7: xxxx A 1 G L -106.682 126.232 177.885 0.000 0.000 0.000 1 0.000 P 1 + F 3 8: xxxx A 2 S L -20.124 24.502 -179.585 0.000 0.000 0.000 1 0.000 P 1 +F 3 9: xxxx A 3 M L -60.092 2.018 -178.549 0.000 0.000 0.000 1 0.000 P 1 F + 3 10: xxxx A 1 G L -115.172 -16.017 -179.179 0.000 0.000 0.000 1 0.000 P + 1 F 4 11: xxxx A 2 S L -143.860 148.913 -179.781 0.000 0.000 0.000 1 0.000 P + 1 F 4 12: xxxx A 3 M L -81.755 -23.354 -174.564 0.000 0.000 0.000 1 0.000 P +1 F 4 13: xxxx A 1 G L -71.907 147.690 -178.617 0.000 0.000 0.000 1 0.000 P +1 F 5 14: xxxx A 2 S L -81.417 52.986 179.983 0.000 0.000 0.000 1 0.000 P 1 +F 5 15: xxxx A 3 M L -83.126 -56.051 178.109 0.000 0.000 0.000 1 0.000 P 1 + F 5 Generating the report: GROUP 1/G AVGS [-108.8372, 73.3412] GROUP 2/S AVGS [-75.969, 10.2868] GROUP 3/M AVGS [-72.0558, 7.1884]

...roboticus

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


In reply to Re: averaging a group of lines with numeric value in a text file by roboticus
in thread averaging a group of lines with numeric value in a text file by Anas

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.