Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Re: a hash and 2 loops--not working by frozenwithjoy
in thread a hash and 2 loops--not working by jhumphreys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (6)
    As of 2014-07-31 05:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (245 votes), past polls