Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone tell me why I am getting an illegal division by zero error on this? The error is with $ctr and it dies in it's present state. When I comment it out and use my $ctr = "4", it works.

As for the values, here's a print out of the only hash key I have so far:

6 fface.gif http://www.go.com 19 8 4

foreach (sort keys %db) { my ($starttime, $filename, $pageurl, $exposure, $clicks) = split(/\ +|\|/, $db{$_}); my $ctr = $clicks / $exposures * 100; #my $ctr = "4"; $ctr =~ s/(\.\d\d)\d*/$1/; print <<"ALL"; <tr> <td>$_</td> <td><center>$filename</center></td> <td><center>$pageurl</center></td> <td><center>$exposure</center></td> <td><center>$clicks</center></td> <td><center>$ctr</center></td> </tr> }

Replies are listed 'Best First'.
Re: illegal division by zero
by Caron (Friar) on Jan 30, 2004 at 08:29 UTC

    There are two reasons for that error:

    • You are splitting on "\|\|", but your string is separated by spaces. It should be split /\s+/,$db{$_}
    • You declared $exposure but you are using $exposures

    Are you using strict?

Re: illegal division by zero
by PodMaster (Abbot) on Jan 30, 2004 at 08:26 UTC
    Perl's error messages are often very self explanatory, like in this case
    E:\>perl print 1/0; Illegal division by zero at - line 1. E:\>
    You cannot divide numbers by 0 (or perl cannot natively). You should either ensure you're not dividing by zero, or use eval{} to trap any exceptions.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: illegal division by zero
by coec (Chaplain) on Jan 30, 2004 at 08:30 UTC
    In your my declaration, you have defined $exposure but you're trying to use $exposures later on (spot the plural). This is why use strict is IMHO mandatory.
Re: illegal division by zero
by pelagic (Priest) on Jan 30, 2004 at 08:36 UTC
    Maybe you should do a proper debugging:
    use Data::Dumper; print Dumper($db{$_}); my ($starttime, $filename, $pageurl, $exposure, $clicks) = split(/\|\| +/, $db{$_}); print Dumper($starttime); print Dumper($filename); print Dumper($pageurl); print Dumper($exposure); print Dumper($clicks);

    then you would exactli know, what the contents of your variables are ...
    Imre
      Maybe you should do a proper debugging

      Or even better:

      print Data::Dumper->Dump([\%db, $starttime, $filename, $pageurl, $exposure, $clicks], [qw(db starttime filename pageurl exposure clicks)]); __OUTPUT__ $db = { 'key' => '6 fface.gif http://www.go.com 19 8 4 ' }; $starttime = '6'; $filename = 'fface.gif'; $pageurl = 'http://www.go.com'; $exposure = '19'; $clicks = '8';
        THAT looks good, but consider answer fron Caron:
        you are using
        $exposures ^

        (plural) which is probably not defined.
        You should definitely use
        use strict;