Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl illegal division by zero
    primarykey
    data
    text
    <p>I am trying to run this Perl program.</p> <pre><code>#!/usr/bin/perl # --------------- exchange.pl ----------------- &amp;read_exchange_rate; # read exchange rate into memory # now let's cycle, asking the user for input... print "Please enter the amount, appending the first letter of the name of\n"; print "the currency that you're using (franc, yen, deutschmark, pound) -\n"; print "the default value is US dollars.\n\n"; print "Amount: "; while (&lt;STDIN&gt;) { ($amnt,$curr) = &amp;breakdown(chop($_)); $baseval = $amnt * (1/$rateof{$curr}); printf("%2.2f USD, ", $baseval * $rateof{'U'}); printf("%2.2f Franc, ", $baseval * $rateof{'F'}); printf("%2.2f DM, ", $baseval * $rateof{'D'}); printf("%2.2f Yen, and ", $baseval * $rateof{'Y'}); printf("%2.2f Pound\n\nAmount: ", $baseval * $rateof{'P'}); } sub breakdown { @line = split(" ", $_); $amnt = $line[0]; if ($#line == 1) { $curr = $line[1]; $curr =~ tr/a-z/A-Z/; # uppercase $curr = substr($curr, 0, 1); # first char only } else { $curr = "U"; } return ($amnt, $curr); } sub read_exchange_rate { open(EXCHRATES, " </code></pre> <p>Whenever it gets to line 17 (<code>$baseval = $amnt * (1/$rateof{$curr})</code>), I get the error <code>Illegal division by zero</code>.</p> <p>What's wrong?</p> <p>I am new to Perl, so please explain your answer.</p> <p>This only happens in Strawberry Perl. ActivePerl works, but it lists all the currency conversions as 0.0.</p> <p>UPDATE: I changed the code to look like this:</p> <pre><code>#!/usr/bin/perl &amp;read_exchange_rate; # read exchange rate into memory # now let's cycle, asking the user for input... print "Please enter the amount, appending the first letter of the name of\n"; print "the currency that you're using (franc, yen, deutschmark, pound) -\n"; print "the default value is US dollars.\n\n"; print "Amount: "; while (&lt;STDIN&gt;) { ($amnt,$curr) = &amp;breakdown(chomp($_)); $baseval = eval { $amnt * (1/$rateof{$curr}) }; printf("%2.2f USD, ", $baseval * $rateof{'U'}); printf("%2.2f Franc, ", $baseval * $rateof{'F'}); printf("%2.2f DM, ", $baseval * $rateof{'D'}); printf("%2.2f Yen, and ", $baseval * $rateof{'Y'}); printf("%2.2f Pound\n\nAmount: ", $baseval * $rateof{'P'}); } sub breakdown { @line = split(" ", $_); $amnt = $line[0]; if ($#line == 1) { $curr = $line[1]; $curr =~ tr/a-z/A-Z/; # uppercase $curr = substr($curr, 0, 1); # first char only } else { $curr = "U"; } return ($amnt, $curr); } sub read_exchange_rate { open EXCHRATES, "&lt;exchange.db" or die "$!\n"; while ( &lt;EXCHRATES&gt; ) { chomp; split; $curr = $_[0]; $val = $_[1]; $rateof{$curr} = $val; } close(EXCHRATES); } </code></pre> <p>Now, I'm getting this in Strawberry Perl when I use Open With(Yes, I'm on Windows): <code>No such file or directory</code> But if I double-click it, it starts fine, but the session looks kind of like this:</p> <pre><code>Please enter the amount, appending the first letter of the name of the currency that you're using (franc, yen, deutschmark, pound) - the default value is US dollars. Amount: 5 y 0.00 USD, 0.00 Franc, 0.00 DM, 0.00 Yen, and 0.00 Pound Amount: </code></pre> <p>Something is obviously wrong. I already changed all instances of <code>chop</code> to <code>chomp</code>. What do I do now?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload