Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by supriyoch_2008 (Scribe)
on May 07, 2012 at 04:05 UTC ( #969185=perlquestion: print w/ replies, xml ) Need Help??
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;

Comment on Why am I getting wrong result in basic arithmetic calculations from d5.pl for $calculation1 and $calculation2?
Select or Download Code
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

    If you use Data::Dumper to print your hash, you might see the bug:

    use Data::Dumper; print Dumper( \%vals );

    You need to split your input lines on semicolons before you split on equals signs.


    Improve your skills with Modern Perl: the free book.

Re: Why am I getting wrong result in basic arithmetic calulations from d5.pl for $calculation1 and $calculation2?
by Cristoforo (Deacon) on May 08, 2012 at 03:30 UTC
    chromatic has found the cause of your problem, but just to show another way to initialize the %vals hash, here is a way that doesn't split but instead, captures the key/value pairs.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %vals = map /(\S+)\s*=\s*(\d+)/g, <DATA>; print Dumper \%vals; __DATA__ x1=2; x2=3; x3=4; y1=3; y2=5; y3=7; z1=4; z2=7; z3=10;

    The output of Data::Dumper is

    C:\Old_Data\perlp>perl t5.pl $VAR1 = { 'y1' => '3', 'z1' => '4', 'x2' => '3', 'z2' => '7', 'x1' => '2', 'x3' => '4', 'z3' => '10', 'y2' => '5', 'y3' => '7' };

    Hope this is of some use, Chris

Re: Why am I getting wrong result in basic arithmetic calulations from d5.pl for $calculation1 and $calculation2?
by Cristoforo (Deacon) on May 08, 2012 at 03:31 UTC
    (Somehow posted twice, in error).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://969185]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2015-07-07 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls