Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: a hash and 2 loops--not working

by frozenwithjoy (Priest)
on Nov 08, 2012 at 21:30 UTC ( #1002989=note: print w/replies, xml ) Need Help??


in reply to a hash and 2 loops--not working

The main problem is that you are never actually setting the values for $to and $from. Below, I've made a couple changes to your script so that these values are set. Also, I made a couple other changes, including adding a note so that the user gets a prompt when entering an unrecognized currency and chomping $value so that the formatting of the output looks as you wanted it to.
#!/usr/bin/env perl # convert3.pl use warnings; use strict; my ( $value, $from, $to, $rate, $rates, %rates ); %rates = ( pounds => 1, USD => 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: "; OUTER0: while (<STDIN>) { chomp; INNER0: for my $cur (keys %rates) { # added keys so you don't loop t +hrough values, too $from = $cur; # set $from currency last OUTER0 if $from eq $_; } print "Currency not recognized, try again: "; # Added to avoid con +fusion when an unknown currency is entered } print "Enter your target currency: "; OUTER1: while (<STDIN>) { chomp; INNER1: for my $cur (keys %rates) { # added keys so you don't loop t +hrough values, too $to = $cur; # set $to currency last OUTER1 if $to eq $_; } print "Currency not recognized, try again: "; # Added to avoid con +fusion when an unknown currency is entered } print "Enter your amount to convert: "; $value = <STDIN>; chomp $value; # added to fix format of output $rate = $rates{$to} / $rates{$from}; print "$value $from is ", $value * $rate, " $to. \n"; __END__ Enter your starting currency: french francs Enter your target currency: swiss francs Enter your amount to convert: 100 100 french francs is 24.3 swiss francs.

Replies are listed 'Best First'.
Re^2: a hash and 2 loops--not working
by jhumphreys (Novice) on Nov 08, 2012 at 22:16 UTC

    frozenwithjoy-

    Thanks for your help!

    Best,

    -j

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002989]
help
Chatterbox?
[davies]: LanX: No. The mayor of London would better be described as the transport commisar for London. S/h/it doesn't have a tenth of the power of La Sturgeon (and therefore does <10% of the damage).
[LanX]: xD ... complicated countrie(s)
LanX reigned by the Duke of Normandy
[davies]: LanX: Politicians making jobs for each other, really. Results in unnecessary complications and tax bills with up to 5 levels of Govt busily inventing new crimes for people to commit.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-12-15 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (441 votes). Check out past polls.

    Notices?