What is the keys() function in Perl?
Overview
In Perl, the keys() function is used to return all the keys of a hash as a list.
Syntax
keys(hash)
Parameters
This function takes the parameter hash. This is the hash we want to get the keys of.
Return value
It returns a list of keys.
Example
# create hashes%hash1 = ('One' => 1,'Two' => 2,'Third' => 3,'Fourth' => 4);%hash2 = ('Edpresso' => 1,'Is' => 2,'Awesome' => 3);# get keys@keys1 = keys(%hash1);@keys2 = keys(%hash2);# print keysprint("Keys are ", join("-", @keys1), "\n");print("Keys are ", join("-", @keys2), "\n");
Explanation
- Line 2–9: We create hashes.
- Line 12 and 13: We use the
keys()function to get the keys of the hashes. - Line 16 and 17: We use the
join()method to separate the keys of the hashes that thekey()function returned.