> -----Original Message-----
> From: David Draley [mailto:[EMAIL PROTECTED]]
> Sent: Friday, September 07, 2001 7:18 AM
> To: [EMAIL PROTECTED]
> Subject: adding array elements
> 
> 
> What would be the best way to add only the even array elements?
> 
> @array[0..9];
> 
> #result of even elements

By "add", do you mean "compute the sum"?

Destructive method:

   $sum = 0;
   $sum += shift @array, shift @array while @array;

Non-destructive method:

   $sum = 0;
   $sum += $a[$_ + $_] for 0 .. @array/2;

I don't know if either of these qualify as "best"

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to