Skip to content

Commit bb50db9

Browse files
committed
some rewriting
1 parent 84cd894 commit bb50db9

File tree

1 file changed

+104
-51
lines changed

1 file changed

+104
-51
lines changed

perl-hashes.tt

Lines changed: 104 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
=title Hashes
2-
=timestamp 2013-03-17T11:12:06
3-
=indexes hash, key, value, associative
1+
=title Hashes in Perl
2+
=timestamp 2013-03-19T11:12:06
3+
=indexes hash, key, value, associative, %, =>, fat arrow, fat comma
44
=status show
55
=books beginner_book
66
=author leprevost
@@ -12,110 +12,163 @@
1212

1313
=abstract start
1414

15-
In this chapter we are going to learn about one of the powerful tools in Perl programming: hashes.
16-
Some times called associative arrays, dictionaries or maps, hashes are one of the basic data structures available in Perl.
15+
In this article of the <a href="/perl-tutorial">Perl Tutorial</a>
16+
we are going to learn about one of the powerful tools in Perl programming: <b>hashes</b>.
17+
Some times called associative arrays, dictionaries or maps, hashes are one of the data structures available in Perl.
1718

1819
=abstract end
1920

20-
Hashes, like other Perl variables, are declared using the <hl>my</hl> keyword. The variable name is preceded by the <hl>%</hl> symbol.
21+
A hash is an un-ordered group of key-value pairs. The keys are uniques strings. The values are scalar values.
22+
Each value can be either a number, a string or a reference. (We'll learn about references later, but those allow
23+
us to build multi-dimensional data strctures.
2124

22-
It's a little mnemonic trick to help you remind about the key-value structure. Don't worry you will get it in a moment.
25+
Hashes, like other Perl variables, are declared using the <hl>my</hl> keyword. The variable name is preceded by the
26+
percentage (<hl>%</hl>) symbol.
2327

24-
Similarly to arrays, hashes can hold a series of elements that can also be retrieved at will, the major difference is the way how this happens. When retrieving an element from an array you should use its <hl>index</hl>. When retrieving elements from hashes, you should use names , or its <hl>keys</hl>.
28+
It's a little mnemonic trick to help you remind about the key-value structure.
2529

26-
Each hash key is associated to a <hl>value</hl> and they are all unique inside the hash structure. That means no repetitive keys are allowed.
30+
Some people think that hashed are like arrays (the old name 'associative array' also indicates this, and in some other
31+
languages like PHP, there is no difference between the two), but there are two major differences between arrays and
32+
hashes. Arrays are ordered, and you access an element of an array using its numerical index.
33+
Hashes are un-ordered and you access a value using a key which is a string.
2734

28-
In practice, when you place inside a hash a pair of key and value, you are placing inside its structure two scalars that are converted to text.
35+
Each hash key is associated with a single <b>value</b> and the keys are all unique inside a single hash structure.
36+
That means no repetitive keys are allowed. (If you want to have more than one values for a key, you'll will need to wait
37+
a bit till we reach the references.)
2938

30-
<h2> Hash basics </h2>
39+
Let's see some code now:
3140

32-
Lets create a hash and see how they work:
41+
<h2>Create an empty hash</h2>
3342

3443
<code lang="perl">
35-
my %hash;
44+
my %color_of;
3645
</code>
3746

38-
In order to access a hash element you should do the following:
47+
<h2>Insert element in a hash</h2>
48+
49+
Insert a key-value pair: In this case 'apple' is the key and 'red' is the associated value.
3950

4051
<code lang="perl">
41-
$hash{$<key_name>};
52+
$color_of{'apple'} = 'red';
4253
</code>
4354

44-
When accessing a specific key-value pair, the hash is called using the <hl>$</hl> symbol because pairs are stored as scalars.
55+
You can also use a variable instead of the key and then you don't need to put the
56+
variable in quotes:
57+
58+
<code lang="perl">
59+
my $fruit = 'apple';
60+
$color_of{$fruit} = 'red';
61+
</code>
4562

46-
Since our hash was create and not populated the code written above will return nothing, so lets place inside the hash a few elements:
63+
Actually, if the key is a simple string, you could leave out the quotes even when you use the string directly:
4764

4865
<code lang="perl">
49-
$hash{“apple”} = “red”;
50-
$hash{“orange”} = “orange”;
51-
$hash{“grape”} = “purple”;
66+
$color_of{apple} = 'red';
5267
</code>
5368

54-
We could also have instantiated the variable with the key-value pairs simultaneous passing to the hash a list of key-value pairs:
69+
As you can see above, when accessing a specific key-value pair, we used the <hl>$</hl> symbol (and not the % symbol)
70+
because we are accessing a single value which is a <b>scalar</b>.
71+
72+
<h2>Fetch and element of a hash</h2>
73+
74+
Quite similar to the way we insterted an element we can also fetch the value of an element.
5575

5676
<code lang="perl">
57-
my %hash = (
58-
“apple” => “red”,
59-
“orange” => “orange”,
60-
“grape” => “purple”,
61-
);
77+
print $color_of{apple};
6278
</code>
6379

64-
This is called the 'big arrow notation', and its used to indicate pairs of elements.
80+
If the key does not exist, the hash will return an <a href="http://perl5maven.com/undef-and-defined-in-perl">undef</a>,
81+
and if <hl>warnings</hl> are enabled, as they should be, then we'll get a
82+
<a href="http://perl5maven.com/use-of-uninitialized-value">warning about uninitialized value</a>.
83+
84+
<code lang="perl">
85+
print $color_of{orange};
86+
</code>
6587

66-
Now, we can do the following in order to access the value of the <i>apple</i> key:
88+
Let's a few more key-value pairs to the hash:
6789

6890
<code lang="perl">
69-
print $hash{“apple”};
91+
$color_of{orange} = “orange”;
92+
$color_of{grape} = “purple”;
7093
</code>
7194

72-
The returned value is :
95+
<h2>Initialize a hash with values</h2>
96+
97+
We could have instantiated the variable with the key-value pairs simultaneous passing to the hash a list of key-value pairs:
7398

7499
<code lang="perl">
75-
> red.
100+
my %color_of = (
101+
“apple” => “red”,
102+
“orange” => “orange”,
103+
“grape” => “purple”,
104+
);
76105
</code>
77106

78-
Now lets see what happens when we include another key-value pair using a already stored key:
107+
This is called the <b>fat arrow</b> or <b>fat comma</b>, and it is used to indicate pairs of elements.
108+
The first name fat arrow will be clear once we see the the other arrow (->) used in Perl.
109+
The name fat comma is used, because these arrows are basically the same as commas. So we could have written this too:
79110

80111
<code lang="perl">
81-
$hash{“apple”} = “green”;
82-
print $hash{“apple”};
112+
my %color_of = (
113+
“apple”, “red”,
114+
“orange”, “orange”,
115+
“grape”, “purple”,
116+
);
83117
</code>
84118

85-
The result will be:
119+
Actually the fat comma allows you to leave out the quotes on the left-hand side writing like this:
120+
86121
<code lang="perl">
87-
> “green”;
122+
my %color_of = (
123+
apple => “red”,
124+
orange => “orange”,
125+
grape => “purple”,
126+
);
88127
</code>
89128

90-
As you can see the value associated to the <hl>apple</hl> key was overwritten, so remember, keys should be unique.
91129

92-
Now you can understand why the <hl>%</hl> symbol, the two little spheres are like pairs of things, or in our case, keys-values.
130+
<h2>Assignment to a hash element</h2>
131+
132+
Let's see what happens when we assign another value to an existing key:
133+
134+
<code lang="perl">
135+
$color_of{apple} = “green”;
136+
print $color_of{“apple”}; # green
137+
</code>
138+
139+
The assignment changed the value associated with the <hl>apple</hl> key. Remember, keys are unique and each key has a
140+
single value.
93141

94-
<h2> Hash size </h2>
142+
<h2>Iterating over hashes</h2>
95143

96-
There is a quick way to know the size of a hash, and for that you have to use the <hl>keys</hl> keyword:
144+
In order to access a value in a hash you need to know the key.
145+
When the keys of a hash are not pre-defined values you can use the <hl>keys</hl> function
146+
to get the list of keys. Then you can iterate over those keys:
97147

98148
<code lang="perl">
99-
my $hash_size = keys %hash;
100-
print $hash_size;
149+
my @fruits = keys %color_of;
150+
for my $fruit (@fruits) {
151+
print “The color of '$fruit' is $color_of{$fruit}\n”;
152+
}
101153
</code>
102154

103-
This will print the number of key-value pairs:
155+
You don't even need to use the temporary varible <hl>@fruits</hl>, you can iterate
156+
over directly the return values of the <hl>keys</hl> function:
104157

105158
<code lang="perl">
106-
> 3
159+
for my $fruit (keys %color_of) {
160+
print “The color of '$fruit' is $color_of{$fruit}\n”;
161+
}
107162
</code>
108163

109-
<h2> Iterating over hashes </h2>
110164

111-
This is how a for loop looks like when iterating over a hash:
165+
<h2>The size of a hash</h2>
166+
167+
When we say the size of a hash, usually we mean the number of key-value pairs.
168+
You can get it by placing the <hl>keys</hl> function in scalar context.
112169

113170
<code lang="perl">
114-
for my $key (keys %hash) {
115-
print “the key is: $key and the value is $hash{$key}”;
116-
}
171+
print scalar keys %hash;
117172
</code>
118173

119-
<h2> Conclusion </h2>
120-
You have learned the basics about one of the most important and powerful data structures in Perl, a little step closer to become a Perl Maven.
121174

0 commit comments

Comments
 (0)