CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Adding a 2D array?

 



spyke212
Novice

Mar 15, 2008, 12:25 PM

Post #1 of 4 (789 views)
Adding a 2D array? Can't Post

Hi,

I have a 2d array consisting of various integers, its dimensions are 50 x 100. Each row, contains 100 values, and my aim is to add up all the values for the 100 columns to create a 1x100 array where each element contains the cumilative values. So all of column 1 would be added together to give one value and so on, until all the columns are complete.

Please help me out, ive been at it for hours, but I can seem to get it to work

Many thanks

S


(This post was edited by spyke212 on Mar 15, 2008, 12:55 PM)


eWish
User

Mar 15, 2008, 7:57 PM

Post #2 of 4 (783 views)
Re: [spyke212] Adding a 2D array? [In reply to] Can't Post

Please show us what you have tried.


hydpm
User

Mar 19, 2008, 8:11 PM

Post #3 of 4 (773 views)
Re: [spyke212] Adding a 2D array? [In reply to] Can't Post

Hi,

Few days back, myself and Kevin gave a similar solution for reading values in multidimentional way in column wise.

You can try modifying that code



Plz : query in perlguru with "multi dimentional array " keyword.


hydpm
User

Mar 19, 2008, 8:29 PM

Post #4 of 4 (771 views)
Re: [spyke212] Adding a 2D array? [In reply to] Can't Post

Hi Spyke,



Modified the old post we made to solve your problem.

Replace with the arrays i used with yours.




@A = (1,2,3,4);
@B = (5,6,7,8);
@AB = ([@A],[@B]);

my $n = $#A;

my $j = 0;
do {
my @t,@f;
foreach my $i (0..$#AB) {
push @t, $AB[$i][$j];
$sum = 0;

foreach (@t) {
$sum = $sum + $_;
}
}
push @f, $sum;
print join(' ',@t),"\n";
}until ($j++ == $n);

print @f;



this i tried with two small arrays.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives