33=indexes hash, key, value, associative, %, =>, fat arrow, fat comma
44=status show
55=books beginner_book
6- =author leprevost
6+ =author szabgab
77=index 1
88=archive 1
99=feed 1
1313=abstract start
1414
1515In 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.
16+ we are going to learn about <b>hashes</b>, one of the powerful parts of Perl.
17+
18+ Some times called associative arrays, dictionaries, or maps; hashes are one of the data structures available in Perl.
1819
1920=abstract end
2021
2122A hash is an un-ordered group of key-value pairs. The keys are unique 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 structures.
23+ Each value can be either a number, a string, or a reference. We'll learn about references later.
2424
2525Hashes, like other Perl variables, are declared using the <hl>my</hl> keyword. The variable name is preceded by the
26- percentage (<hl>%</hl>) symbol .
26+ percentage (<hl>%</hl>) sign .
2727
2828It's a little mnemonic trick to help you remind about the key-value structure.
2929
3030Some 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.
31+ languages, such as PHP, there is no difference between arrays and hashes. ), but there are two major differences between arrays
32+ and hashes. Arrays are ordered, and you access an element of an array using its numerical index.
3333Hashes are un-ordered and you access a value using a key which is a string.
3434
3535Each 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.)
36+ That means no repetitive keys are allowed. (If you really, really want to have more than one values for a key,
37+ you'll will need to wait a bit till we reach the references.)
3838
3939Let's see some code now:
4040
@@ -44,9 +44,9 @@ Let's see some code now:
4444my %color_of;
4545</code>
4646
47- <h2>Insert element in a hash</h2>
47+ <h2>Insert a key-value pair into a hash</h2>
4848
49- Insert a key-value pair: In this case 'apple' is the key and 'red' is the associated value.
49+ In this case 'apple' is the key and 'red' is the associated value.
5050
5151<code lang="perl">
5252$color_of{'apple'} = 'red';
@@ -66,12 +66,12 @@ Actually, if the key is a simple string, you could leave out the quotes even whe
6666$color_of{apple} = 'red';
6767</code>
6868
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>.
69+ As you can see above, when accessing a specific key-value pair, we used the <hl>$</hl> sign (and not the % sign )
70+ because we are accessing a single value which is a <b>scalar</b>. The key is placed in curly braces.
7171
72- <h2>Fetch and element of a hash</h2>
72+ <h2>Fetch an element of a hash</h2>
7373
74- Quite similar to the way we inserted an element we can also fetch the value of an element.
74+ Quite similar to the way we inserted an element, we can also fetch the value of an element.
7575
7676<code lang="perl">
7777print $color_of{apple};
@@ -104,9 +104,9 @@ my %color_of = (
104104);
105105</code>
106106
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:
107+ <hl>=></hl> 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, thinner arrow (->) used in Perl.
109+ The name fat comma comes from the fact that these arrows are basically the same as commas. So we could have written this too:
110110
111111<code lang="perl">
112112my %color_of = (
@@ -116,7 +116,8 @@ my %color_of = (
116116);
117117</code>
118118
119- Actually the fat comma allows you to leave out the quotes on the left-hand side writing like this:
119+ Actually, the fat comma allows you to leave out the quotes on the left-hand side makig the code cleaner
120+ and more readable.
120121
121122<code lang="perl">
122123my %color_of = (
@@ -126,7 +127,6 @@ my %color_of = (
126127);
127128</code>
128129
129-
130130<h2>Assignment to a hash element</h2>
131131
132132Let's see what happens when we assign another value to an existing key:
@@ -136,7 +136,7 @@ $color_of{apple} = "green";
136136print $color_of{apple}; # green
137137</code>
138138
139- The assignment changed the value associated with the <hl >apple</hl > key. Remember, keys are unique and each key has a
139+ The assignment changed the value associated with the <b >apple</b > key. Remember, keys are unique and each key has a
140140single value.
141141
142142<h2>Iterating over hashes</h2>
@@ -171,4 +171,10 @@ You can get it by placing the <hl>keys</hl> function in scalar context.
171171print scalar keys %hash;
172172</code>
173173
174+ <h2>Thanks</h2>
175+
176+ The first edition of this article was written by <a href="http://www.leprevost.com.br/">Felipe da Veiga Leprevost</a> who also makes the
177+ <a href="http://br.perl5maven.com/">Portuguese translation</a> of the Perl Maven articles.
178+
179+
174180
0 commit comments