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

Thanks, yes agreed. My code was simplified and there are multiple key/value pairs in the actual data.
  • Comment on Re^2: Can I avoid loops getting the single value from a hash without knowing the key?

Replies are listed 'Best First'.
Re^3: Can I avoid loops getting the single value from a hash without knowing the key?
by hexcoder (Curate) on Jun 13, 2014 at 22:12 UTC
    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.