Reading — step 1 of 5
Learn
~1 min readLists, Arrays, and Hashes
Hashes are unordered key-value collections:
my %ages = (
alice => 30,
bob => 25,
carol => 40,
);
print $ages{alice}, "\n"; # 30 — $ sigil for one element
$ages{dan} = 50; # add
delete $ages{bob}; # remove
exists $ages{alice}; # true (defined and present)
my @keys = keys %ages; # all keys
my @vals = values %ages;
for my $name (sort keys %ages) {
print "$name: $ages{$name}\n";
}
The => ("fat arrow") is just a fancy comma that auto-quotes the left side. So (alice => 30) is the same as ("alice", 30).
Hashes are how you build records, lookups, counters, and most non-trivial data:
my %word_count;
$word_count{$_}++ for split /\s+/, $text;
That's a complete word counter — split on whitespace, increment hash entry per word.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…