468,110 Members | 1,921 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

Perl math problem..

I am having a problem in a perl script that I can't seem to find an
answer for..

The $cost and $retail vars come from another part of the script and
would be something like 000134.345 and 000220.202 respectively..

I an then trying to use these values in a calculation like this..

$profit = ($retail - $cost) / $retail * 100;

But I get the following error..

../avgprofit.pl PriceList.txt
Illegal division by zero at ./avgprofit.pl line 35, <> line 1.

If is substitute the values directly into the calculation it works
perfectly..

Anyone got any ideas?

Thanks..
Oct 7 '05 #1
6 10834
WipeOut wrote:
I am having a problem in a perl script that I can't seem to find an
answer for..

The $cost and $retail vars come from another part of the script and
would be something like 000134.345 and 000220.202 respectively..

I an then trying to use these values in a calculation like this..

$profit = ($retail - $cost) / $retail * 100;

But I get the following error..

./avgprofit.pl PriceList.txt
Illegal division by zero at ./avgprofit.pl line 35, <> line 1.

If is substitute the values directly into the calculation it works
perfectly..

Anyone got any ideas?

Thanks..


I'd help, but i'd need to see the rest of the file.

What the problem mostlikely is, is that the variable $retail and $cost
were initialized inside a sub or such. Try putting at the very top of
your perl file something like this.

my $retail = 0;
my $cost = 0;

if you need to change some stuff do so. But it seems like it's not
reading it as a global but a local variable.

Daniel
Oct 7 '05 #2
Daniel Moree wrote:
WipeOut wrote:
I am having a problem in a perl script that I can't seem to find an
answer for..

The $cost and $retail vars come from another part of the script and
would be something like 000134.345 and 000220.202 respectively..

I an then trying to use these values in a calculation like this..

$profit = ($retail - $cost) / $retail * 100;

But I get the following error..

./avgprofit.pl PriceList.txt
Illegal division by zero at ./avgprofit.pl line 35, <> line 1.

If is substitute the values directly into the calculation it works
perfectly..

Anyone got any ideas?

Thanks..

I'd help, but i'd need to see the rest of the file.

What the problem mostlikely is, is that the variable $retail and $cost
were initialized inside a sub or such. Try putting at the very top of
your perl file something like this.

my $retail = 0;
my $cost = 0;

if you need to change some stuff do so. But it seems like it's not
reading it as a global but a local variable.

Daniel


Hi Daniel..

Its a very simple script for taking a fixed length database file and
creating a CSV created from my limited perl ability.. :)

Thanks for your help..

Here is the whole script (may be some line wrapping)..

#!/usr/bin/perl

#Set up first line field desc.
#print "\"PRODUCTCODE\";\"PRODUCT\";\"COST_PRICE\";\"PRIC E\";\"PROFIT\";\n";

# Read from standard input or file specified on command line.
while (<>) {

# Specify each field as a or A with its width.
# Use a to preserve trailing spaces, A to strip them.
@fields = unpack('A18 A30 A1 A9 A9 A3 A2 A18 A2 A11 A2 A9 A3 A9 A3 A9
A3 A1 A4 A7 A1 A1', $_);

# Insert decimal point into price fields.
$fields[3] =~ s/(...)$/.$1/;
$fields[4] =~ s/(...)$/.$1/;

# Clean up and substitutions.
foreach $f (@fields) {
# Replace \'s with /'s
$f =~ s/(\\)/\//g;
# Remove leading spaces
$f =~ s/^\s//;
# Double any " characters, and then enclose in "
$f =~ s/"/""/g;
}

# Profit calculation.
$cost = $fields[3];
$retail = $fields[4];
$profit = ($retail - $cost) / $retail * 100;
#$profit = (220.202 - 000134.567) / 220.202 * 100;

# Print out seperated list except where price field is less than
000005.000 (zero).
if ($fields[4] gt "000005.000") {
# "PRODUCTCODE";"PRODUCT";"COST_PRICE";"PRICE;"PROFI T"
print "$fields[0];$fields[1];$cost;$retail;$profit;\n";
}
}

#end
Oct 7 '05 #3
# Profit calculation.
$cost = $fields[3];
$retail = $fields[4];
$profit = ($retail - $cost) / $retail * 100;
#$profit = (220.202 - 000134.567) / 220.202 * 100;

This section looks good, I'd try something like this just to see what
info is actually in $cost and $retail try this

print "FIELDS[3]: $fields[3]\n";
print "FIELDS[4]: $fields[4]\n";
$cost = $fields[3];
$retail = $fields[4];
$profit = ($retail - $cost) / $retail * 100;
#$profit = (220.202 - 000134.567) / 220.202 * 100;

this should print out what's in the fields array sections. The unpack
could cause no data to enter the array. Just check what's really in
those fields, if nothing, then check the data input($_), if there is
data in the input, then the unpack function isn't working right

Daniel
Oct 7 '05 #4
Daniel Moree wrote:
# Profit calculation.
$cost = $fields[3];
$retail = $fields[4];
$profit = ($retail - $cost) / $retail * 100;
#$profit = (220.202 - 000134.567) / 220.202 * 100;

This section looks good, I'd try something like this just to see what
info is actually in $cost and $retail try this

print "FIELDS[3]: $fields[3]\n";
print "FIELDS[4]: $fields[4]\n";
$cost = $fields[3];
$retail = $fields[4];
$profit = ($retail - $cost) / $retail * 100;
#$profit = (220.202 - 000134.567) / 220.202 * 100;

this should print out what's in the fields array sections. The unpack
could cause no data to enter the array. Just check what's really in
those fields, if nothing, then check the data input($_), if there is
data in the input, then the unpack function isn't working right

Daniel


I use the $cost and $retail vars further down when "print"ing the line
to create the CSV format output and they are working fine..

Thats why it is so strange that the calculation doesn't work with the
vars but when a value is used in place of it that is a similar value to
the one contained in the var it doesn't work..

Also I tried braking it down..

$profit = ($retail - $cost); #this works

$profit = ($retail - $cost) / $retail; #this doesn't

...so something to do with the division is broken when using a var.. :(
Oct 7 '05 #5
WipeOut wrote:
Also I tried braking it down..

$profit = ($retail - $cost); #this works

$profit = ($retail - $cost) / $retail; #this doesn't

..so something to do with the division is broken when using a var.. :(


How can you say that?
You haven't proven to us that $retail _isn't_ zero.

warn "Error: \$retail is not defined" unless defined $retail;
warn "Error: \$retail is zero" if $retail == 0;
$profit = ($retail - $cost) / $retail;

-Joe
Oct 8 '05 #6
Joe Smith wrote:
WipeOut wrote:
Also I tried braking it down..

$profit = ($retail - $cost); #this works

$profit = ($retail - $cost) / $retail; #this doesn't

..so something to do with the division is broken when using a var.. :(

How can you say that?
You haven't proven to us that $retail _isn't_ zero.

warn "Error: \$retail is not defined" unless defined $retail;
warn "Error: \$retail is zero" if $retail == 0;
$profit = ($retail - $cost) / $retail;

-Joe

Cracked it.. :)

Basically this script is a manipulation of another one I wrote and what
I had overlooked is that I have an "if" statement further down to ignore
anything with a value less than 000005.000, which would include zero's..

I moved the calculation below that and it works fine now..

Just another example of the error being between the keyboard and the
seat.. :)

Daniel and Joe, thanks for you help..
Oct 8 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

58 posts views Thread by @ | last post: by
2 posts views Thread by TSO | last post: by
3 posts views Thread by Chris Pruett | last post: by
20 posts views Thread by Xah Lee | last post: by
reply views Thread by Xah Lee | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
13 posts views Thread by squash | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.