Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Can I avoid loops getting the single value from a hash without knowing the key?

by Laurent_R (Parson)
on Jun 10, 2014 at 06:16 UTC ( #1089385=note: print w/ replies, xml ) Need Help??


in reply to Can I avoid loops getting the single value from a hash without knowing the key?

You have received good responses to your question, but I would like to expand a little the scope: if your hash is always going to have only one key-value pair, then may be you don't really need this hash and you could start your data structure one level down.


Comment on Re: Can I avoid loops getting the single value from a hash without knowing the key?
Re^2: Can I avoid loops getting the single value from a hash without knowing the key?
by anadem (Scribe) on Jun 12, 2014 at 22:19 UTC
    Thanks, yes agreed. My code was simplified and there are multiple key/value pairs in the actual data.
      Hmm, maybe I misunderstood, but this is difficult to follow. On one hand you ask for a value of a hash with one entry, OTOH you state this situation is simplified, in your real code there are more entries.

      But if there are multiple entries in the hash, LanX's suggestion will put the value of a randomly choosen hash entry in $value. This might not be what you want.

      So I suggest defensive coding:
      first check if the hash only holds one entry if (1 == scalar keys %hash) before (blindly) retrieving a value.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-09-22 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (191 votes), past polls