search instagram arrow-down

Archives

Categories

Meta

Hash

Introduction
A hash is a two-dimensional array which contains keys and values, they’re sometimes called “associative arrays”, or “lookup tables”. Instead of looking up items in a hash by an array index, you can look up values by their keys.

Entire hashes are denoted by ’%’:
%days               # (key1, val1, key2, val2 …)
A Hash represents a set of key/value pairs.
my %fruit color = (“apple”, “red”, “banana”, “yellow”).
You can use whitespace and the “=>” operator to lay them out more nicely:
my %fruit_color = (
apple  => “red”,
banana => “yellow”,
);
To get at hash elements:
$fruit_color{“apple”};           # gives “red”
You can get at lists of keys and values with “keys()” and “values()”.

my @fruits = keys %fruit_colors;
my @colors = values %fruit_colors;
$days{‘Feb’}        # the ‘Feb’ value from hash %days

accessing the HASH Element
To access the hash element use the syntax like: ”’$hash{$some_key}”’ This is similar to what we used for array but here we used curly braces {} instead of square brackets. []

This entry was posted in Perl.
Leave a Reply
Your email address will not be published. Required fields are marked *

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: