For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > trouble using <STDIN> to access hash values









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author trouble using <STDIN> to access hash values
Joseph Uri Podwol

2005-06-06, 3:56 pm

I am just starting to learn Perl using the book Beginning Perl by Simon
Cozens.
http://learn.perl.org/library/beginning_perl/

In ch 4 pg 3, there is a program in which I am to create a currency
converter. I start by creating a hash with various currencies and their
relative values. I use <STDIN> to enter my country of origin, destination
country, and the amount of my native currency. Then I write two formulas
to calculate the value of my native currency in my destination country.

my ($value, $from, $to, $rate, %rates);
%rates=(
pounds => 1,
dollars => 1.6,
marks => 3.0,
"french francs" => 10.0,
yen => 174.8,
"swiss francs" =>2.43,
drachma => 492.3,
euro =>1.5
);

print "Enter your starting currency: ";
$from = <STDIN>;
print "Enter your target currency: ";
$to = <STDIN>;
print "Enter your amount: ";
$value = <STDIN>;

chomp($from,$to,$value);
$rate = $rates{$to} / $rates{from};

print "$value $from is", $value*$rate," $to.\n";

In the book, running the program returns the following (using * .* to
indicate inputed values):
Enter your starting currency: *dollars*
Enter your target currency: *euro*
Enter your amount: *200*
200 dollars is 187.5 euro.
>


However, when I run the program, I get:
Enter your starting currency: *dollars*
Enter your target currency: *euro*
Enter your amount: *200*
Use of uninitialized value in division (/) at convert1.plx line 26,
<STDIN> line 3.
Illegal division by zero at convert1.plx line 26, <STDIN> line 3.

There appears to be a problem in how <STDIN> retuns hash values in my
version 5.8 that is not present in the author's version. Any suggestions?

Thankfully,
Joe

Steve McQ

2005-06-06, 3:56 pm

On Mon, 6 Jun 2005 11:41:59 -0400 (EDT)
"Joseph Uri Podwol" <jup2@cornell.edu> wrote:

> $rate = $rates{$to} / $rates{from};


from should be $from.

--
Steve McQ | http://www.tigertrails.com
steve@tigertrails.com | Linux Registered User: 294367
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com