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
my %person = (name => "Alex", age => 36);print "$person{name}\n";Storing key-value pairs with % and accessing with $h{key}.
Simple enough for a 5-year-old to understand
my %person = (name => "Alex", age => 36);print "$person{name}\n";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.
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.
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";
}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.
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.
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.
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.