Back-End Programming Exam  >  Back-End Programming Videos  >  Perl Building Blocks: An Introduction to Perl  >  Perl Tutorial - 15: Obtaining Keys & Values from Hashes

Perl Tutorial - 15: Obtaining Keys & Values from Hashes Video Lecture | Perl Building Blocks: An Introduction to Perl - Back-End Programming

57 videos

FAQs on Perl Tutorial - 15: Obtaining Keys & Values from Hashes Video Lecture - Perl Building Blocks: An Introduction to Perl - Back-End Programming

1. How can I obtain all the keys from a hash in Perl?
Ans. To obtain all the keys from a hash in Perl, you can use the `keys` function. It returns a list of all the keys of the hash. Here's an example: ``` my %hash = (name => "John", age => 25, city => "New York"); my @keys = keys %hash; print @keys; # Output: nameagecity ```
2. How can I obtain all the values from a hash in Perl?
Ans. To obtain all the values from a hash in Perl, you can use the `values` function. It returns a list of all the values of the hash. Here's an example: ``` my %hash = (name => "John", age => 25, city => "New York"); my @values = values %hash; print @values; # Output: John25New York ```
3. Can I obtain both the keys and values from a hash in Perl?
Ans. Yes, you can obtain both the keys and values from a hash in Perl using the `each` function. It returns a list containing two elements: the key and the corresponding value. Here's an example: ``` my %hash = (name => "John", age => 25, city => "New York"); while (my ($key, $value) = each %hash) { print "$key: $value\n"; } ``` Output: ``` name: John age: 25 city: New York ```
4. How can I check if a specific key exists in a hash in Perl?
Ans. To check if a specific key exists in a hash in Perl, you can use the `exists` function. It returns true if the key exists in the hash, and false otherwise. Here's an example: ``` my %hash = (name => "John", age => 25, city => "New York"); if (exists $hash{name}) { print "Key 'name' exists in the hash.\n"; } else { print "Key 'name' does not exist in the hash.\n"; } ``` Output: ``` Key 'name' exists in the hash. ```
5. How can I obtain the number of key-value pairs in a hash in Perl?
Ans. To obtain the number of key-value pairs in a hash in Perl, you can use the `scalar` function in combination with the `keys` function. The `keys` function returns a list of all the keys, and the `scalar` function returns the size of the list. Here's an example: ``` my %hash = (name => "John", age => 25, city => "New York"); my $count = scalar keys %hash; print "Number of key-value pairs: $count\n"; ``` Output: ``` Number of key-value pairs: 3 ```
57 videos
Explore Courses for Back-End Programming exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

Sample Paper

,

practice quizzes

,

Perl Tutorial - 15: Obtaining Keys & Values from Hashes Video Lecture | Perl Building Blocks: An Introduction to Perl - Back-End Programming

,

Perl Tutorial - 15: Obtaining Keys & Values from Hashes Video Lecture | Perl Building Blocks: An Introduction to Perl - Back-End Programming

,

Free

,

MCQs

,

Previous Year Questions with Solutions

,

pdf

,

mock tests for examination

,

Perl Tutorial - 15: Obtaining Keys & Values from Hashes Video Lecture | Perl Building Blocks: An Introduction to Perl - Back-End Programming

,

study material

,

Summary

,

Exam

,

Objective type Questions

,

ppt

,

Extra Questions

,

shortcuts and tricks

,

Semester Notes

,

Important questions

,

video lectures

,

Viva Questions

;