Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

keys
perl
hash
communitycreator

What is the keys() function in Perl?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 keys
print("Keys are ", join("-", @keys1), "\n");
print("Keys are ", join("-", @keys2), "\n");
Get the keys of a hash in Perl

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 the key() function returned.

RELATED TAGS

keys
perl
hash
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring