Hi PerlMonks,

This has reference to my question "How can I enter the data of different variables from a text file into a perl program?" posted a few days ago. I have used the code suggested by a perlmonk and I have got correct result from d6.pl (using a little modification for data entry from the text file data.txt). But When I used the line 15 $calculation2 in d5.pl, I got wrong result. Cmd screen shows error like "Illegal division by zero at C:\Users\xyz\Desktop\d5.pl line 15". I think no zero is involved in calculation of $calculation2. I am really confused where I have made the mistake in writing the code of d5.pl. I am looking forward to suggestions from perlmonks so that d5.pl gives correct result for $calculation1 and $calculation2 i.e. 6 and 2. I have given the text file data.txt, code of d6.pl and its correct result, and also the code of d5.pl and its wrong result in calculating the $calculation1 and $calculation2.

The data file, data.txt, has the values of 9 variables (x1 to z3) in 3 separate lines:

x1=2; x2=3; x3=4; y1=3; y2=5; y3=7; z1=4; z2=7; z3=10;

d6.pl has the code:

#!usr/bin/perl-w print"\n Type the name of data file: "; $data=<STDIN>; chomp $data; unless (open (DATA,$data)){ print "\n\n Couldn't open file $data."; } my %vals; while ( <DATA> ) { my ($identifier,$value) = split /\s*=\s*/; $vals {$identifier} = $value; } close DATA; $calculation1=$vals{x1}*$vals{y1}; print"\n\n 1st result=$calculation1;\n\n"; exit;

d6.pl has given correct result for $calculation 1 i.e.:

C:\Users\xyz\Desktop>d6.pl Type the name of data file: data.txt 1st result=6;

But d5.pl is same as d6.pl except line 15 and has the code:

#!usr/bin/perl-w print"\n Type the name of data file: "; $data=<STDIN>; chomp $data; unless (open (DATA,$data)){ print "\n\n Couldn't open file $data."; } my %vals; while ( <DATA> ) { my ($identifier,$value) = split /\s*=\s*/; $vals {$identifier} = $value; } close DATA; $calculation1=$vals{x1}*$vals{y1}; $calculation2=($vals{x1}*$vals{y1})/($vals{x2});# Line 15 print"\n\n 1st result=$calculation1;\n\n 2nd result=$calculation2"; exit;

I have got wrong result from d5.pl for $calculation1 and $calculation2:

C:\Users\xyz>cd desktop C:\Users\xyz\Desktop>d5.pl Type the name of data file: data.txt Illegal division by zero at C:\Users\xyz\Desktop\d5.pl line 15.

Correct results should be:

1st result=6; 2nd result=2;

In reply to Why am I getting wrong result in basic arithmetic calculations from d5.pl for $calculation1 and $calculation2? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.