in reply to how to find a key from a hash

If there's one key in the hash, keys(%hash) will return a single element array (excuse me, I've gone into lecture mode...).

If you really don't care about which key you get from a hash with more than one entry, wouldn't

$key = shift(keys(%hash));
($key) = keys(%hash);

be perfectly adequate?

Incidentally, if there is going to be only one key in a hash or, if you've multiple keys and don't care which one you get, why are you using a hash?

Thanks to Perl Mouse for pointing out a horrid error on my part; for some inane reason I thought shift(keys(%hash)) was valid Perl.

emc

Replies are listed 'Best First'.
Re^2: how to find a key from a hash
by Perl Mouse (Chaplain) on Nov 21, 2005 at 16:04 UTC
    wouldn't
    $key = shift(keys(%hash));
    be perfectly adequate?
    How could something that doesn't even have a remote chance of compiling be "perfectly adequate"?

    shift, but it's nature, must have an lvalue as its argument, and to be more precise, an lvalue that starts with an @. List context keys() is neither. shift isn't an alternative for [0].

    Perl --((8:>*

      Because I went into idiot mode; it tends to happen often on Mondays.

      emc