Solved

Perl divide by zero problem with varibles extracted from text

Posted on 2008-10-20
6
802 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
Comment Utility
$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
Comment Utility
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 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
Blank is treated as 0 when used in arithmetic
$. is the input line number.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 84

Expert Comment

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

Author Comment

by:zzhang2006
Comment Utility
Great! I solved my problem. Thanks
0
 
LVL 1

Author Comment

by:zzhang2006
Comment Utility
Great!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now