Perl Hash Variables


A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name followed by the "key" associated with the value in curly brackets.

Here is a simple example of using hash variables −

Example

 Live Demo

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

Output

This will produce the following result −

$data{'John Paul'} = 45
$data{'Lisa'} = 30
$data{'Kumar'} = 40

Updated on: 28-Nov-2019

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements