# Perl divide by zero problem with varibles extracted from text

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
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
\$fields[1] must be 0 to cause the error.

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

before line 151 and post the output.
0
ProfessorAuthor Commented:
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
Commented:
Blank is treated as 0 when used in arithmetic
\$. is the input line number.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
perldoc-q "How do I determine whether a scalar is a number/whole/integer/float"
0
ProfessorAuthor Commented:
Great! I solved my problem. Thanks
0
ProfessorAuthor Commented:
Great!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.