Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: a hash and 2 loops--not working

by TomDLux (Vicar)
on Nov 08, 2012 at 22:06 UTC ( [id://1002999]=note: print w/replies, xml ) Need Help??


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

If you want to put label on a loop so you can use 'next', 'last', 'redo', there's no need to label loops you don't go to. Also, I would suggest using a more meaningful name. Instead of calling the labels OUTER0 and OUTER1, how about naming them SOURCE_CURRENCY and TARGET_CURRENCY?

Going through the elements of %rates isn't what you want to do. The whole point of a hash is to look up keys and determine the corresponding value. Just like the point of an array is to look up the i-th element. I would suggest:

if ( ! defined $rates{$_} { print "Don't know how to handle unrecognized rate '$_'.\n"; # handle error in some way. } print $rate{$_}

But since the code is becomming longer, i would definitely suggest using a named variable to store user input, such as $source_currency and $target_currency.

Also, using %rates the way you have, generates a list of a key and it's corresponding value, another key with it's value, for some random ordering of keys. If you really wanted to look at the keys one by one, you can use keys %rates to generate just that list. Similarly, you can use values %rates to generate a list of just the values. The keys and values lists will be in the same order, but may not be the same as you would get on a different machine, or even if add or delete an element.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

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

    TomDLux-

    Excellent feedback. Thanks for your help.

    -j

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1002999]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-19 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found