Duong Nguyen <[EMAIL PROTECTED]> wrote: : : Thanks for the response. I discovered that it was my : @array1[0..5] =1; initialization. : So I manually allocated values to the array to see if it : would work, much to my surprise, the correct "total" was : being printed. Here's what I did: : : change @array1[0..5] to @array1[0] = 1; : @array1[1] = 1; : @array1[2] = 1; : : This works well.
Looks like a lot of Jscript I have been recently unraveling. I'd hate to see that used to initialize 10,000 elements. my @array; @array[ 0 .. 5 ] = (1) x 6; my @totals = 0; : for($i=0; $i<4; $i++) : { In perl, is it rare that you would need to use indexes to iterate through a single array. I don't care much for $i and $j, either. I didn't come from a C background, so the idiom above tends to confuse me. foreach $amount ( @array[ 0 .. 3 ] ) { : if($i == 0) : { : @total[$i] = @array1[$i]; : print "First group"; : print @total[$i]; : } My first reaction was why not place this outside the loop? In perl both @total[$1] and $total[$1] will both work, but the second form is preferred. You will also catch a lot less grief from this list. : else : { : $j = $i - 1; : print "Second group"; : @total[$i] = @total[$j] + @array1[$i]; : print @total[$i]; : } : } Since we are not using an $i index, $j won't help much. We can grab values from an array by using a negative index. -1 is the last element in an array: push @totals, $totals[ -1 ] + $amount; print $totals[ -1 ]; } Let's review that. I am pushing onto @totals the sum of the last value on @totals ( $totals[-1] ) and the current amount. Then I print the new last total on @totals. Notice that this algorithm will allow any range in $amounts to be totaled, not just consecutive amounts starting from the beginning. HTH, Charles K. Clarkson -- Head Bottle Washer, Clarkson Energy Homes, Inc. Mobile Home Specialists 254 968-8328 As tested: #!/usr/bin/perl use strict; use warnings; my @array; @array[ 0 .. 5 ] = (1) x 6; my @totals = 0; foreach my $amount ( @array[ 0 .. 3 ] ) { push @totals, $totals[ -1 ] + $amount; } shift @totals; print @totals; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>