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.   
  ----- Original Message ----- 
  From: u235sentinel 
  To: Duong Nguyen ; [EMAIL PROTECTED] 
  Sent: Thursday, December 25, 2003 2:40 AM
  Subject: Re: Perl Help: Array Manipulation


  I'm a newbie to perl also.  Been workign with it for a whole 2 weeks 
  now.  Actually.. make that 3 ::grinz::

  Warning.. lengthy email below.. I beg forgiveness in advance :-)

  Ok.  Here is what I did.

  #!/usr/bin/perl

  @array1[0..5] = 1;
  @total[0] = 0;

  for($i=0; $i<4; $i++)
  {
       if($i == 0)
       {
            @total[$i] = @array1[$i];
          print "First group";
            print @total[$i];
       }
       else
       {
            $j = $i - 1;
          print "Second group";
            @total[$i] = @total[$j] + @array1[$i];
            print @total[$i];
       }
   }


  When I run it I get the following:

  First group1Second group1Second group1Second group1

  So technically it's not even printing the total of your array.  The 
  array is fine.  Every container is populated with the #1.  You are 
  basically printing each container entry in the array.  If you are 
  looking for a total of all containers in the @total array you need to 
  add them together in the array.  You will need to add them to a scalar 
  variable or a particular container (total[5] perhaps?).  Here is what I 
  did.  Warning.. not a clean bit of code but it get's the idea across.. I 
  think:

  #!/usr/bin/perl

  @array1[0..5] = 1;
  @total[0] = 0;

  for($i=0; $i<4; $i++)
  {
       if($i == 0)
       {
            @total[$i] = @array1[$i];
          print "First group";
            print @total[$i];
          $total = $total + @total[$i];
          print "\n";
          print $total;
       }
       else
       {
            $j = $i - 1;
          print "Second group";
            @total[$i] = @total[$j] + @array1[$i];
          $total = $total + @total[$i];
            print @total[$i];
          print "\n";
          print $total;
       }
   }

  So now you see on the left the sum of each container in the array. 

  Ok.. I away the flogging.  Someone is bound to have done a better job 
  explaining this.  At least I can blame it on being up after midnight ;-)

  Speaking of which... MERRY CHRISTMAS TO ALL!!!

  Night.

Reply via email to