473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 10993
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: @ | last post by:
A benchmark in 2002 showed PHP is much slower in shell or when Apache has Mod_Perl. With the new PHP kissing Java's ass, Perl is once again the #1 CGI choice. Java is for a big team in short...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
2
by: TSO | last post by:
Hi there, I've recently tried to translate some Perl code into Python - code is below. Is there a more Pythonic form?
3
by: Chris Pruett | last post by:
An odd (to me) behavior I was hoping someone could explain. Take the simple command: perl -e 'print 0xCEC5 + 0xFFFFE253 & 0xFFFF' I tried this on a number of computers. Most give 0xFFFF, ...
1
by: limelight | last post by:
I have discovered a math error in the .NET framework's Log function. It returns incorrect results for varying powers of 2 that depend on whether the program is run from within the IDE or from the...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
13
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.