in reply to Can you spot the problem?

I've been bitten by a similar issue once:
my $avg = 0; if (my ($x, $y) = $str =~ m!(\d+)/(\d+)!) { $avg = $x / $y if $y; }
That died with a 'division by zero' error. Can you spot why?

Abigail

Replies are listed 'Best First'.
Re: Re: Can you spot the problem?
by BrowserUk (Patriarch) on Mar 09, 2004 at 13:17 UTC

    Because zero can have leading zeros?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Yep, and that's why chanting "don't compare to 0" when someone writes $y == 0 isn't always appropriate. My code would have run fine if the test had been unless $y == 0.

      Abigail