Hello Monks:

I need help with a slight variation of a code example I found in an O'relly book, regarding arrays of arrays.
(http://lux.e-reading.bz/htmbook.php/orelly/perl4/prog/ch09_01.htm#FOOTNOTE-1).

The book explains "we omit (for clarity) the my declarations that you would ordinarily put in."

The snippet I have reads a text file with the following content:

10 20 30 40 50
15 25 35
1 2 3 4 5 6 7 8 9 10


If I need the sum total from each line this would be the code where @AoA contains the above three lines:

## Load values into the Array ### $Test_File = "test_file.txt"; open (TESTFILE, "$Test_File"); while (<TESTFILE>) { @tmp = split; # Split elements into an array. push @AoA, [ @tmp ]; # Add an anonymous array reference t +o @AoA. } close (TESTFILE); ## Print the totals for each line ### for $i ( 0 .. $#AoA ) { $row = $AoA[$i]; for $j ( 0 .. $#{$row} ) { $Total_Balance[$i] += "$row->[$j]"; } print "the total is: ($Total_Balance[$i])<hr>"; }

The output for the above code is:

         the total is: (150)
         the total is: (75)
         the total is: (55)


I need the same output but I changed the text file a little bit, (the row number is in the left and the value to add up is in the right), as follows:

1|10
1|20
1|30
1|40
1|50
2|15
2|25
2|35
3|1
3|2
3|3
3|4
3|5
3|6
3|7
3|8
3|9
3|10


For this example I only have three anonymous arrays but there could be 10 or 30 (unknown)
Thanx beforehand for your help


In reply to array of arrays by virtualweb

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.