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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |