All:
I am wondering what general approach you would use to solve the following problem:
  • You have a loop of some sort
  • In the body of the loop, an array is built by doing a lot of calculations
  • For the purpose of making a uniform column output - you push the value to an array or use 0 for a placeholder
  • At the end of the loop, you would like to print out totals for each column

    For instance:

    #!/usr/bin/perl -w use strict; $\ = "\n"; foreach my $row ( 1 .. 10 ) { my @output; foreach my $column ( 1 .. 10 ) { push @output , $column; } print join ',' , @output; } # print column totals here
    Now this is pretty easy to do by just looking at it, there will be exactly 10 rows and 10 columns. Each column will be the same number so the total will be 10, 20, 30, etc.

    My question is how would you do this if the number of columns was fixed, but the number of rows might vary depending on your data. Furthermore - the code to populate the the @output array is complicated and isn't as easy to determine as the simple foreach loop in my example. I was thinking of an AoA, but I can't get my head wrapped around how to process it afterwards.

    Any thoughts?

    Thanks in advance - L~R


    In reply to Multi-Column Totals by Limbic~Region

    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.