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
A hash in Perl is an unordered collection of key-value pairs. Unlike arrays, which use integers as their index, hashes use strings as their index (keys). Here's a comprehensive tutorial on Perl hashes:
You can create a hash by assigning key-value pairs using the =>
operator:
my %fruit_colors = ( apple => 'red', banana => 'yellow', grape => 'purple' );
Access a value by referring to its key:
print $fruit_colors{'apple'}; # Outputs: red
$fruit_colors{'orange'} = 'orange'; # Adds a new key-value pair $fruit_colors{'apple'} = 'green'; # Modifies the value for the key 'apple'
Use the delete
function:
delete $fruit_colors{'banana'};
Iterate over each key-value pair:
foreach my $fruit (keys %fruit_colors) { print "$fruit is $fruit_colors{$fruit}\n"; }
Use the exists
function:
if (exists $fruit_colors{'apple'}) { print "Apple is in the hash!\n"; }
my @fruits = keys %fruit_colors; # Gets all the keys my @colors = values %fruit_colors; # Gets all the values
Determine the number of key-value pairs:
my $number_of_fruits = keys %fruit_colors;
Extracting multiple values using a list of keys:
my @selected_colors = @fruit_colors{'apple', 'grape'};
my %more_fruits = ( kiwi => 'green', lemon => 'yellow' ); @fruit_colors{ keys %more_fruits } = values %more_fruits;
You can create a reference to a hash and then access the hash through the reference:
my $hash_ref = \%fruit_colors; print $hash_ref->{'apple'}; # Outputs: green
Unlike arrays, there's no guarantee on the order of elements in a hash. If you need ordered key-value pairs, consider using the Tie::IxHash
module from CPAN.
each
: Returns both the key and value of the next pair.reverse
: In the context of a hash, it swaps keys and values. Be cautious; if values aren't unique, you might lose some data.sort
: Since hashes are unordered, you often need to sort the keys to process the hash in a specific order.Hashes in Perl are versatile and straightforward to use. They provide efficient key-based access to values and offer a range of built-in functions for manipulation. Use them whenever you need to associate keys and values.
Hash data structure basics:
# Creating a hash my %person = ( name => 'John', age => 30, country => 'USA', ); # Accessing values using keys my $name = $person{'name'}; print "Name: $name\n";
Hash tables introduction and usage:
# Creating a hash table my %fruit_color = ( apple => 'red', banana => 'yellow', orange => 'orange', ); # Accessing values using keys my $color = $fruit_color{'banana'}; print "Color of banana: $color\n";
Associative arrays in Perl explained:
# Creating an associative array my %grades = ( 'Alice' => 90, 'Bob' => 85, 'Charlie' => 95, ); # Accessing values using keys my $alice_grade = $grades{'Alice'}; print "Alice's grade: $alice_grade\n";
Hash functions and key-value pairs:
# Creating a hash with hash function my %colors = ( 'red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF', ); # Accessing values using keys my $hex_code = $colors{'blue'}; print "Hex code for blue: $hex_code\n";
Perl hash data structure overview:
# Creating a hash my %book = ( title => 'Perl Programming', author => 'John Doe', pages => 300, ); # Accessing values using keys my $title = $book{'title'}; print "Book Title: $title\n";
Hash vs. arrays in programming:
# Using an array my @fruits = ('apple', 'banana', 'orange'); # Using a hash my %fruit_color = ( apple => 'red', banana => 'yellow', orange => 'orange', );
Working with hashes in Perl examples:
# Creating a hash my %employee = ( name => 'Alice', age => 25, role => 'Developer', ); # Adding a new key-value pair $employee{'salary'} = 50000; # Deleting a key-value pair delete $employee{'age'}; # Iterating through keys and values foreach my $key (keys %employee) { my $value = $employee{$key}; print "$key: $value\n"; }