Accessing Hash Elements in Perl

PERLServer Side ProgrammingProgramming Scripts

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
raja
Updated on 29-Nov-2019 05:42:13

Advertisements