Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Hashes in Perl are very flexible and provide a variety of operations to manipulate, access, and utilize them efficiently. This tutorial will cover essential operations you can perform on hashes.
You can define a hash using a list of key-value pairs:
my %fruit_colors = ( apple => 'red', banana => 'yellow', grape => 'purple' );
To retrieve the value for a specific key:
print $fruit_colors{'apple'}; # Outputs: red
To add a new key-value pair or modify an existing one:
$fruit_colors{'orange'} = 'orange'; # Adds a new pair $fruit_colors{'apple'} = 'green'; # Modifies an existing pair
To remove a key-value pair from the hash:
delete $fruit_colors{'banana'};
To loop through all key-value pairs:
foreach my $fruit (keys %fruit_colors) { print "$fruit is $fruit_colors{$fruit}\n"; }
To see if a particular key exists in the hash:
if (exists $fruit_colors{'apple'}) { print "Apple exists in the hash.\n"; }
To get all keys or values:
my @fruits = keys %fruit_colors; my @colors = values %fruit_colors;
To find out the number of key-value pairs in the hash:
my $size = keys %fruit_colors;
To combine two hashes:
my %more_fruits = ( kiwi => 'green', lemon => 'yellow' ); @fruit_colors{ keys %more_fruits } = values %more_fruits;
To extract specific key-value pairs from a hash:
my @selected_colors = @fruit_colors{'apple', 'grape'};
If values are unique, you can swap keys and values:
my %inverted_hash = reverse %fruit_colors;
You can also use references to hashes:
my $hash_ref = \%fruit_colors; print $hash_ref->{'apple'}; # Outputs: green
To empty a hash completely:
%fruit_colors = ();
Hashes in Perl are powerful tools for associating keys and values. The operations mentioned above provide a wide range of functionalities to work with hashes efficiently. While using them, it's essential to understand the context (list or scalar) and use the operations appropriately.
Manipulating Perl hashes:
my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Adding elements $fruit_color{'orange'} = 'orange'; # Deleting keys delete $fruit_color{'banana'}; # Iterating through the hash for my $fruit (keys %fruit_color) { my $color = $fruit_color{$fruit}; print "$fruit is $color\n"; } # Sorting the hash by keys my %sorted_fruit_color = map { $_ => $fruit_color{$_} } sort keys %fruit_color; # Merging hashes my %more_fruit = ('grape' => 'purple', 'watermelon' => 'green'); my %combined = (%fruit_color, %more_fruit); # Accessing values my $apple_color = $fruit_color{'apple'}; # Checking key existence if (exists $fruit_color{'banana'}) { print "Banana exists in the hash\n"; }
Adding elements to a hash in Perl:
hash{key} = value
syntax.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Adding elements $fruit_color{'orange'} = 'orange'; $fruit_color{'grape'} = 'purple';
Deleting keys from a Perl hash:
delete
keyword to remove keys and their associated values from a hash.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Deleting keys delete $fruit_color{'banana'};
Iterating through a Perl hash:
for
loop with the keys
function to iterate through the keys of a hash and access their corresponding values.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Iterating through the hash for my $fruit (keys %fruit_color) { my $color = $fruit_color{$fruit}; print "$fruit is $color\n"; }
Sorting a hash in Perl:
sort
function with keys
and create a new hash with the desired order.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Sorting the hash by keys my %sorted_fruit_color = map { $_ => $fruit_color{$_} } sort keys %fruit_color;
Merging and combining hashes in Perl:
%combined = (%hash1, %hash2)
syntax, merging their key-value pairs.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); my %more_fruit = ('grape' => 'purple', 'watermelon' => 'green'); # Merging hashes my %combined = (%fruit_color, %more_fruit);
Accessing values in a Perl hash:
$hash{key}
to retrieve the value associated with a specific key.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Accessing values my $apple_color = $fruit_color{'apple'};
Checking if a key exists in a Perl hash:
exists
function to check if a specific key exists in a hash before attempting to access its value.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Checking key existence if (exists $fruit_color{'banana'}) { print "Banana exists in the hash\n"; }
Hash operations with Perl built-in functions:
keys
, values
, and each
for working with hash keys, values, and key-value pairs.my %fruit_color = ('apple' => 'red', 'banana' => 'yellow'); # Getting keys and values my @keys = keys %fruit_color; my @values = values %fruit_color; # Using each to iterate key-value pairs while (my ($fruit, $color) = each %fruit_color) { print "$fruit is $color\n"; }