keys, values, exists and delete

keys and values

keys returns the list of keys, values returns the list of values, and both come back in the same internal order. Walk one list and index the hash when you need the pair.

my %user = (name => "Ada", age => 36); for my $key (sort keys %user) { print "$key = $user{$key}\n"; } print scalar keys %user, "\n"; # 2

perl demo.pl

age = 36
name = Ada
2

In scalar context keys returns a count, not a list. That is why the usual test for an empty hash is if (keys %h).

exists and defined

exists asks whether a key is present; defined asks whether its value is a real value. A key can exist and hold undef at the same time, so the two questions are different.

my %user = (name => "Ada", age => 36); $user{phone} = undef; print "key present\n" if exists $user{phone}; print "no value yet\n" unless defined $user{phone};

perl demo.pl

key present
no value yet

Without exists, a condition that looks inside a nested key autovivifies the level above it, as lesson 3.3 warned: if ($user{prefs}{theme}) creates prefs even when the branch does nothing. Reach for exists whenever a wrong branch would change the data.

delete

delete removes the pair and returns the value that was stored, which makes it useful for draining a table one entry at a time. Deleting a missing key is not an error; it returns undef.

my %user = (name => "Ada", age => 36, phone => undef); my $old = delete $user{age}; print "removed $old\n"; # removed 36 print "keys: ", scalar keys %user, "\n"; # keys: 2

perl demo.pl

removed 36
keys: 2

To empty a whole hash at once, assign an empty list: %user = (); That is faster than deleting key by key and keeps the variable declared.

Invert a hash

Swapping keys and values is a single expression, because a hash assigned from a list takes alternating key and value. Duplicate values collapse, so invert only when values are unique.

my %by_id = ( a => 1, b => 2, ); my %by_num = reverse %by_id; print $by_num{2}, "\n"; # b

perl demo.pl

b

reverse on a hash flattens it to key-value pairs, flips them, and the assignment rebuilds a hash. One built-in, no loop.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. In what order do keys and values return their lists?

    Answer

    The internal order of the hash, and both lists agree with each other

    A hash has no promised order, but the two lists come from the same table walk, so the nth key belongs to the nth value.

  2. What does scalar keys %h give you?

    Answer

    The number of keys, because keys in scalar context is a count

    In scalar context keys returns a count, which is why if (keys %h) is the usual test for an empty hash.

  3. A key exists and its value is undef. What do exists and defined say?

    Answer

    exists is true and defined is false

    The pair is in the hash, so the key is present, but the value is the absence of a value.

  4. What does delete $h{missing} do?

    Answer

    It returns undef and leaves the hash as it was

    Deleting a missing key is not an error; the call returns the value that was there, which is undef.

  5. How do you empty a whole hash and keep the variable declared?

    Answer

    %h = ();

    Assigning the empty list rebuilds the hash empty, which is faster than deleting key by key.

  6. When is my %by_num = reverse %by_id; wrong?

    Answer

    When two keys hold the same value, because only one of them survives as a key

    The inverted hash has one entry per value, so duplicate values collapse into a single key.

  7. What does this program print? my %h = (b => 2, a => 1); print join("-", sort keys %h), "\n";

    Answer

    a-b

    sort orders the keys as strings, so a comes before b whatever their values are.

  8. What does this program print? my %h = (a => 1); delete $h{a}; print scalar keys %h, "\n";

    Answer

    0

    The only pair is removed, so the hash has no keys left and the count is 0.

  9. What does this program print? my %h = (a => 1, b => 2); my %r = reverse %h; print $r{2}, "\n";

    Answer

    b

    The reverse turns the pair b => 2 into 2 => b, and the lookup finds the b.

  10. What does this program print? my %h = (name => "Ada"); $h{age} = undef; print "present\n" if exists $h{age}; print "value\n" if defined $h{age};

    Answer

    present

    The age key is in the hash, but its value is undef, so only the exists branch prints.

Article author: Arthur Isaev