http://qs321.pair.com?node_id=266699


in reply to Adding and Multplying Elements in an Array

Using an undef variable in a mathematical operation causes its value to begin at zero. I don't see a formal explanation in perlop, but I would speculate that under warnings or -w you see the message regarding $sum2 being uninitialized because zero times anything is zero, and perl probably figures that you want to know in advance that you're performing a computation guaranteed to result in zero. Addition works because adding to zero isn't a problem. The solution therefore is to initialize $sum2 with a non-zero value.


"The dead do not recognize context" -- Kai, Lexx
  • Comment on Re: Adding and Multplying Elements in an Array

Replies are listed 'Best First'.
Re: Re: Adding and Multplying Elements in an Array
by Dru (Hermit) on Jun 18, 2003 at 01:47 UTC
    I set  $sum2 = 1 and that seem to do it. Thanks.
      yes that's the way to tackle..... suresh