supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why am I getting wrong result in basic arithmetic calulations from d5.pl for $calculation1 and $calculation2?
by chromatic (Archbishop) on May 07, 2012 at 05:47 UTC | |
|
Re: Why am I getting wrong result in basic arithmetic calulations from d5.pl for $calculation1 and $calculation2?
by Cristoforo (Curate) on May 08, 2012 at 03:30 UTC | |
|
Re: Why am I getting wrong result in basic arithmetic calulations from d5.pl for $calculation1 and $calculation2?
by Cristoforo (Curate) on May 08, 2012 at 03:31 UTC |