Hashes

This lecture notes is based on Prof Doreen De Leon's lectures.

Reading

Learning Perl, Chapter 5 Hashes.

Definition

Hash Variables

$table{100} = 256; # "100" is key, 256 is value
$table{"abc"} = "def"; # "abc" is key, "def" is value
$table{100}+=3; #259
print $table{"abc"}, "\n"; # def
print $table{100}, "\n"; #259
for($i = 1; $i < 10; $i++) {
    $square_table{$i} = $i * $i; 
}

print $square_table{5}; #25
$capital{"USA"} = "Washington D.C.";
$capital{"France"} = "Paris";
$capital{"China"} = "Beijing";
$capital{"Japan"} = "Tokyo";
$capital{"Spain"} = "Madrid";

print $capital{"USA"}, "\n"; # Washington D.C.
print $capital{"Canada"},"\n"; # undef

Alternate Initialization

$capital{"France"} = "Paris";
$capital{"China"} = "Beijing";
$capital{"Japan"} = "Tokyo";
@array = %capital;
# array becomes
# France Paris China Beijing Japan Tokyo

print "@array";
%capital = qw(France Paris China Beijing Japan Tokyo);

print $capital{"France"},"\n" ; # Paris
print $capital{"China"}, "\n"; # Beijing
@array = ("111222333","Charles", "222333555", "Mary");
%student = @array;

print $student{"111222333"}; #Charles

The Big Arrow Operator (=>)

%capital = ("France" => "Paris", "China" => "Beijing", "Japan" => "Tokyo");
# actually you can remove the quotes
# %capital = (France => Paris, China => Beijing, Japan => Tokyo);
print $capital{"France"}; #Paris

General Notes

%capital = ("France" => "Paris", "China" => "Beijing", "Japan" => "Tokyo");
%country = reverse %capital;

print $country{"Paris"}; #France

Hash Functions

  1. keys    Syntax: keys %hash
  2. values    Syntax: values %hash
  3. each    Syntax: each %hash
  4. delete   Syntax: delete $hash{$key};
  5. exists   Syntax: exists $hash{$key};
%capital = ("France" => "Paris", "China" => "Beijing", "Japan" => "Tokyo");
@countries = keys(%capital); # France, China, Japan

foreach $country(@countries) {
    print "The capital of $country is $capital{$country}.\n";
}

# Output
# The capital of France is Paris.
# The capital of China is Beijing.
# The capital of Japan is Tokyo.
%capital = ("France" => "Paris", "China" => "Beijing", "Japan" => "Tokyo");
@cities = values(%capital);
print "@cities"; #Paris Beijing Tokyo
%capital = ("France" => "Paris", "China" => "Beijing", "Japan" => "Tokyo");

while (($country, $city) = each(%capital)) {
    print "The capital of $country is $city.\n";
}

# Output
# The capital of France is Paris.
# The capital of China is Beijing.
# The capital of Japan is Tokyo.
%capital = ("France" => "Paris", "China" => "Beijing", "Japan" => "Tokyo");

if(exists($capital{"France"})) { # true
    print "The key France exists.\n";
} else {
    print "The key France doesn\'t exists.\n";
}

delete $capital{"France"};

if(exists($capital{"France"})) { # false
    print "The key France exists.\n";
} else {
    print "The key France doesn\'t exists.\n";
}

Hash Slices

($scores{"Amy"}, $scores{"Ben"}, $scores{"Cathy"}) = (55, 66, 77);
print $scores{"Ben"},"\n"; # 66

@scores{"Dave", "Eva", "Fred"} = (71, 81, 91);
print $scores{"Eva"}, "\n"; # 81

@names = ("Gloria", "Heather", "Ivy");
@scores{@names} = (23, 34, 45);
print $scores{"Ivy"}, "\n"; # 45;

@names2 = qw(Ben Fred Dave);
print "@scores{@names2}\n"; # 66 91 71

($scores2{"Jen"}, $scores2{"Ken"}, $scores{"Ivy"}) = (10,20,30);

%new = (%scores, %scores2); # join 2 score set
print $new{"Gloria"},"\n"; # 23
print $new{"Ivy"},"\n"; # 30
print $new{"Ken"}, "\n"; # 20

# another way to join 2 hash
# faster than the previous method
@scores{keys %scores2} = values %scores2;
print $scores{"Gloria"},"\n"; # 23
print $scores{"Ivy"},"\n"; # 30
print $scores{"Ken"},"\n"; # 20