Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Perl divide by zero problem with varibles extracted from text

Posted on 2008-10-20
6
Medium Priority
?
826 Views
Last Modified: 2013-12-12
I have the following code, with give a dividing by zero problem. Obviously, the $fields[1] is not supposed to be zero. It print out a decimal number when direct print out. And it can be added to another varible with + and arithmetic is fine. But when it is in the denominator it it cuase such a message:

Illegal division by zero at E:\CruzerLock2\Stocks\quote2.pl line 151, <GEN0> lin
e 13.

The code segment look like this:


my $change = sprintf "%.2f",  ( $fields[4]) / ($fields[1])*100;



Is there something hidden in the fields[1] that couse this? Thanks
0
Comment
Question by:zzhang2006
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 22764142
$fields[1] must be 0 to cause the error.

Add a

print "$. $fields[1]\n";

before line 151 and post the output.
0
 
LVL 1

Author Comment

by:zzhang2006
ID: 22764311
You may be right, I change it to

 print "See this->  $. $fields[1] \n";

             my $change = sprintf "%.2f",
               ( $fields[4]) /$fields[1]*100;
Then it printed:

See this->  13
Illegal division by zero at E:\CruzerLock2\Stocks\quote2.pl line 152, <GEN0> lin
e 13.

Does a blank means zero? Where is the "13" come from? How can I put a condition to check $fields[1] not zero or simply being a number? Thanks
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 22764361
Blank is treated as 0 when used in arithmetic
$. is the input line number.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:ozo
ID: 22764364
perldoc-q "How do I determine whether a scalar is a number/whole/integer/float"
0
 
LVL 1

Author Comment

by:zzhang2006
ID: 22764429
Great! I solved my problem. Thanks
0
 
LVL 1

Author Comment

by:zzhang2006
ID: 22764431
Great!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally wrote this article to compare SARDU and YUMI, but have now added Easy2Boot, since that is the one I currently use and find the easiest to create and alter.
Web hosting control panels were first developed to make it faster and easier for most users to set up and operate websites. The graphical user interface (GUI) allows users to perform tasks by pointing and clicking rather than typing highly specific…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question