- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking for Key/Value Existence in Perl Hash
If you try to access a key/value pair from a hash in Perl that doesn't exist, you'll normally get the undefined value, and if you have warnings switched on, then you'll get a warning generated at run time. You can get around this by using the exists function, which returns true if the named key exists, irrespective of what its value might be −
Example
#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); if( exists($data{'Lisa'} ) ) { print "Lisa is $data{'Lisa'} years old\n"; } else { print "I don't know age of Lisa\n"; }
Here we have introduced the IF...ELSE statement, which we will study in a separate chapter. For now, you just assume that if( condition ) part will be executed only when the given condition is true otherwise else part will be executed. So when we execute the above program, it produces the following result because here the given condition exists($data{'Lisa'} returns true −
Output
Lisa is 30 years old
- Related Articles
- How to check if a Perl hash already contains a key?
- Perl Hash Variables
- Accessing Hash Elements in Perl
- Getting Hash Size in Perl
- Check for the existence of a key in Java IdentityHashMap
- Test for existence of nested JavaScript object key in JavaScript
- Check for the existence of key in an object using AngularJS
- Extracting Keys and Values from Hash in Perl
- Checking existence of all continents in array of objects in JavaScript
- Check for the existence of a value in Java IdentityHashMap
- Golang program to get key based on value from the hash collection
- MySQL get hash value for each row?
- Golang program to get value from the hash collection based on specified key
- Lock & Key problem using Hash-map
- How to get hash code for the specified key of a Hashtable in C#?

Advertisements