HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PerlOperatorsStringsArrays (@)Hashes (%)Conditions (if)LoopsFunctions (sub)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
📋 Array & Hash
🔁 Condition & Loop
🧩 Function (sub)
Perl›Hashes (%)

Hashes (%)

Storing key-value pairs with % and accessing with $h{key}.

Simple enough for a 5-year-old to understand

Create and access a Hash

Example code
1
my %person = (name => "Alex", age => 36);
% = hash, pairs with =>.
Data% = hash, pairs with =>.
2
print "$person{name}\n";
Value for $h{key}: Alex
Output / InputValue for $h{key}: Alex
DataOutput / Input
💡 Hint: You define a hash with % but access the value with $h{key} ($ is the sigil!).

About this topic

A hash is a data structure that holds key-value pairs and is represented in Perl with names starting with a percent sign. Keys are text, and values can be scalars. The value corresponding to a key is accessed using curly braces.

The sign rule applies here as well: you access the entire hash with a percent sign and a single value with a dollar sign. A dollar sign is used because what you are accessing is a scalar.

Hashes are unordered. When you list the keys, the order is not the order in which you added them, and it can even change between runs. If you want ordered output, you need to explicitly sort the keys.

What you will learn

  • Defining hashes and accessing values
  • Adding and deleting keys
  • Checking for the existence of keys
  • Iterating over a hash
  • The consequences of being unordered

Who is this for?

  • Those who will hold configuration data
  • Those who will perform counting and grouping operations
  • Those who rely on the order of hash output

When to use?

Hashes are used wherever fast access by key is required: frequency tables, configuration sets, ID-to-record mapping.

If order is important, a hash alone is not enough; you need to keep the keys in a separate array or sort them on every listing.

Common mistakes

Wrong

my %yas = ("ali", 30, "ayse", 25);
print %yas{"ali"};

A dollar sign must be used when accessing a single value; the percent sign refers to the entire hash, and this code will result in a syntax error. The sign rule applies to hashes just as it does to arrays.

Correct

my %yas = ("ali", 30, "ayse", 25);
print $yas{"ali"};

Wrong

my %ayar = (renk => "blue");
if ($ayar{"boyut"}) {
  print "size exists\n";
}

Accessing a non-existent key does not throw an error, it returns an undefined value. But this check can be misleading: even if the key exists, if its value is zero or empty text, the condition evaluates to false. A separate function must be used to test the existence of a key.

Correct

my %ayar = (renk => "blue");
if (exists $ayar{"boyut"}) {
  print "size exists\n";
}

Wrong

my %sayilar = (bir => 1, iki => 2, uc => 3);
foreach my $k (keys %sayilar) {
  print "$k\n";
}

There is no error here, but the expectation might be wrong: the output does not come in the order of insertion and can be different on each run. Hashes are unordered. If you want consistent output, you need to sort the keys.

Correct

my %sayilar = (bir => 1, iki => 2, uc => 3);
foreach my $k (sort keys %sayilar) {
  print "$k\n";
}

Frequently asked questions

What is the difference between exists and defined?

exists tests whether the key exists, while defined tests whether its value is defined. A key can exist while its value is undefined; confusing the two leads to logic errors.

How do I delete a key from a hash?

The delete function is used. Assigning undefined to the value does not delete the key, it only empties its value; afterwards, the existence check still returns true. This distinction is frequently confused.

How do I iterate over keys and values together?

Listing the keys and accessing the value for each is the most readable way. The each function also returns a pair, but because it maintains internal state, it behaves unexpectedly in nested usage; it is not preferred in modern code.

Summary

Hashes hold key-value pairs.

A single value is accessed with a dollar sign.

Hashes are unordered.

Key existence is tested with exists.

Deletion is done with delete.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code