Find key name in hash with only one key?

后端 未结 7 520
鱼传尺愫
鱼传尺愫 2021-02-03 22:37

If I have a hash

my %h = (
    secret => 1;
);

and I know that is only is one key in the hash, but I don\'t know what it is called.

相关标签:
7条回答
  • 2021-02-03 23:18

    [ keys %h ]->[0] will also do the disambiguation Joel mentions in an earlier comment. This code smells like it will cause problems though. If there is really only a single key/value pair, there might be a better way to handle the data.

    At the least, I'd check to be sure the expectation is never violated silently. E.g.‐

    keys %h == 1 or die "ETOOMANYKEYS";
    print [ keys %h ]->[0], $/;
    
    0 讨论(0)
提交回复
热议问题