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.
Do I then have to iterate through that hash
my $key; foreach my $i (keys %h) { $key = $h{$i}; }
Or are there a better way to get the name of the key?
my %h = ( secret => 1; );
Extracting Keys and Values from a Hash variable The list of all the keys from a hash is provided by the keys function, in the syntax: keys %hashname . The list of all the values from a hash is provided by the values function, in the syntax: values %hashname . Both the keys and values function return an array.
hash.fetch(key) { | key | block } Returns a value from hash for the given key. If the key can't be found, and there are no other arguments, it raises an IndexError exception; if default is given, it is returned; if the optional block is specified, its result is returned.
A list slice should do it
(keys %h)[0]
keys
returns a list, so just extract the first element of that list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With