Perl Hashes
Working with hashes (associative arrays)
Creating Hashes
my %hash = ( # create hash
name => "John",
age => 30
);
my %ages = ("Alice", 25, "Bob", 30); # list form
Accessing Values
$hash{name} # access value by key
$hash{city} = "NYC"; # add/update key-value
delete $hash{age}; # remove key
Hash Functions
keys(%hash) # get all keys
values(%hash) # get all values
exists($hash{name}) # check if key exists
defined($hash{name}) # check if value defined
Iterating Hashes
foreach my $key (keys %hash) { # iterate keys
print "$key: $hash{$key}\n";
}
while ((my $key, my $value) = each %hash) { # iterate pairs
print "$key: $value\n";
}