If you can look it up by name, you want a hash. The keys to a hash must be unique, but the values can be any scalar.
Sometimes you'll still see people call them "associative arrays", but don't think of them as arrays.
Create a hash with a list of key/value pairs.
    my %stooges = (
        'Moe', 'Howard',
        'Larry', 'Fine',
        'Curly', 'Howard',
        'Iggy', 'Pop',
    );
The => is a "fat comma", and is identical to a comma, plus it quotes the previous word:
    my %stooges = (
        Moe => 'Howard',
        Larry => 'Fine',
        Curly => 'Howard',
        Iggy => 'Pop',
    );
A hash in list context becomes a list of key/value pairs.
    my @hash_as_an_array = %stooges;
    # Contains ( 'Curly', 'Haward', 'Larry', 'Fine', etc... )
Just as the order of keys and values from a hash is effectively random, the order of the key/value pairs in a flattened hash is effectively random.
Get hash values with squiggly braces instead of square braces.
    print $stooges{'Iggy'};
    # Prints "Pop"
Set values that way, too.
    $stooges{'Shemp'} = 'Howard';
Overwrite existing values:
    $stooges{'Iggy'} = 'Ignatowski';
Delete an entry from the hash:
    delete $stooges{'Curly'};
Note that delete does not delete a file. unlink does that.
    unlink $stooges{'Moe'};
    # Deletes a file called 'Howard';
Use the keys and values keywords:
    my @stooge_first_names = keys %stooges;
    my @stooge_last_names = values %stooges;
The order of the keys and values are guaranteed to match up to each other.
If your hash key is a single word, you don't need quotes around it.
    $stooges{Curly} = 'Howard';
The values in a Perl hash may only be scalars. It is not possible to contain an array or a list in an array.
    $hash{comedians} = @stooges;
    # Assigns the length of @stooges to the value
If you want to store an array in a hash, you'll need to use references.
The order of keys %hash and values %hash is effectively random. They will differ between runs of the program. It is not related to the order in which things were added.
The idea of "sorting a hash" does not exist in Perl, because hashes are unordered. You can sort the keys of a hash, or the values of a hash, as they are just lists.
To combine two hashes, look at them as lists and assign them to a hash.
    my %new_hash = (%hash1, %hash2);
The right-hand side of the equals is a long list of key/value pairs from both of the hashes. The list is then assigned to %new_hash. If any keys in %hash2 duplicate keys in %hash1, the %hash2 key/value pair take priority because they're assigned later.
If you have things in a line, ordered, in a sequence, use an array.
If you have a bag of things, unordered, that you want to look up, use a hash.
defined and existsUse defined to see whether a hash element has a value other than undef. It will return true if the hash element has any value other than undef, even 0 and "" (the empty string), which evaluate to false.
Use exists to see whether a hash element has ever been initialized, even if it is not defined (i.e., it has the value 'undef').
    my %h;
    $h{'foo'} = undef;
    defined $h{'foo'} ? print 1 : print 0;
    # $h{'foo'} is not defined, so it prints 0
    exists $h{'foo'} ? print 1 : print 0;
    # but it has been initialized nonetheless, and so this line prints 1
A hash element can be true only if it's defined. It can be defined only if it exists.
However, a hash element can exist without being defined. This means it will not return true even though it exists.
    if ( $h{'foo'} ) {
        print 'true';
    }
    else {
        print 'false';
    }
    # prints 'false'; since $h{'foo'} is not defined, it cannot be true
Submit a PR to github.com/petdance/perl101

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.