I am reading a doc I found online about Perl, I am learning it for a project, and I was studying code that converts currency. The code:

#!/usr/bin/perl
 use warnings;
 use strict;

 my ($number_to_convert, $first_currency, $second_currency, $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 first currency: ";
$first_currency = <>;
print "Enter second currency: ";
$second_currency = <>;
print "Enter amount you wish to convert from ", $first_currency, " to ", $second_currency, "\n";
$number_to_convert = <>;
chomp($first_currency,$second_currency,$number_to_convert);
#Line 23 is a convinience measure, not mandatory
$rate = $rates{$second_currency} / $rates{$first_currency};
print $number_to_convert," ",$first_currency," is ",$number_to_convert*$rate," ",$second_currency, "\n";

What I had a question about was this line:

$rate = $rates{$second_currency} / $rates{$first_currency};

What I understand is that the hash called "rate" takes the second currency entered by the user and divides it by the first currency entered by the user. I wanted clarification on if this is how you would verify that what the user typed in was from the hash, how Perl knows what 2 currencies the uesr is converting, and [ANSWERED SEE EDIT]how I could tell the user that what they entered was not recognized (say if they misspelled "dollars" or something)[ANSWERED SEE EDIT]. Also, would the syntax be the same for strings and numbers?
EDIT:
Upon further reading my question about the misspelling was answered, however I have another question. How do you divide a string by a string?

Recommended Answers

All 5 Replies

hello dsplayer14,

First of, Line 23, is NOT just a convinience measure. The chomp function removes the trailing "\n" from your input from STDIN.
Commet out line 23, and see the error message, you get.

Secondly, it would be goood if your script is kept in a loop, like a while loop anb only exit on certain input. Instead of RE-RUNNING your programming for every operation.

Lastly, why would you want to DIVIDE string with string? And what do you hope to arrive with as your output?

Hope this helps

Thanks for you response, 2teez!
I guess I had a brain fart with that comment, I knew what it did, but at the time I thought it wasnt necessary, thanks for catching that.
Line 25, right, well in the documentation I am reading (which is right here, around page 6), a program I am supposed to make converts currency (similar to the program above, I just changed variable names, really). Frankly, and I cant answer why I would want to divide a string by a string, it's a question I have myself! It's in the documentation, so I assume its a vital part of the program, but I just dont understand how you can divide a word by word. That is what that line is indicating, correct?

Well, I had a few other questions, but I found answers to them. One of them was how Perl knew that the 2 variables $first_currency and $second_currency from the hash %rates. I figured that it's pretty simple, you just specify that the value of those 2 variables is located in the hash itself, and Perl magically did the rest.
Hope I worded this correctly...

Also created a loop to allow the user to run the program again, if he/she wanted to. Here is the updated code:

#!/usr/bin/perl
 use warnings;
 use strict;

 my ($number_to_convert, $first_currency, $second_currency, $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
);

while (1 == 1) {
print "Enter first currency: ";
$first_currency = <>;
print "Enter second currency: ";
$second_currency = <>;
print "Enter amount you wish to convert from ", $first_currency, " to ", $second_currency, "\n";
$number_to_convert = <>;
chomp($first_currency,$second_currency,$number_to_convert);
if ($rates{$second_currency} eq "") {
    die "Unknown currency ",$second_currency,"\n",
}
if ($rates{$first_currency} eq "") {
    die "Unknown currency ",$first_currency,"\n";
}
$rate = $rates{$second_currency} / $rates{$first_currency};
print $number_to_convert," ",$first_currency," is ",$number_to_convert*$rate," ",$second_currency, "\n";
}

Hi,
I really don't get the dividing of word by word thing. And I don't know the documentation you are referring to. I checked the link you provided. The book you are read is a very good Perl beginners book. Though dated now, but you should still be able to get some meat from it.

Hash in Perl is really associative-array. The value is tied to the key. So that once the key is sited, the values could be used. And really, you cannot have more than one particular key. So, the magic in perl is done by that. Declare the key, you get the value.

If I may suggest some improvement on the last improve code posted.

Declare your variable where they are needed. Not at the top of the file. That may be obtainable writing the C language, but not so with Perl.

To get an infinte loop in while-loop use while(1){..} instead of while(1==1){...}
Think of it what is (1==1) ?

It would have shorten the number of lines you wrote if you have done this:

print "Enter first currency: ";
chomp($first_currency = <>);
print "Enter second currency: ";
chomp($second_currency = <>};

instead of doing it later. Like you did.

HOpe this help

Thanks for the tips! I'm going to mark this as solved. I seem to have figured out the questions to all of my answers except that divison one... I'll just let it go. Hopefully it'll be answered down the road. Well, again, than you for your help 2teez!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.