Accessing Hash Elements in Perl


When accessing individual elements from a hash in Perl, you must prefix the variable with a dollar sign ($) and then append the element key within curly brackets after the name of the variable. For example −

Example

 Live Demo

#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
print "$data{'John Paul'}\n";
print "$data{'Lisa'}\n";
print "$data{'Kumar'}\n";

Output

This will produce the following result −

45
30
40

Updated on: 29-Nov-2019

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements