Skip to content

Commit c2640d3

Browse files
committed
add perl-arrays article
1 parent 692b0ce commit c2640d3

File tree

3 files changed

+222
-10
lines changed

3 files changed

+222
-10
lines changed

for-loop-in-perl.tt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
=title The for loop in Perl
2-
=timestamp 2013-01-17T14:45:56
3-
=tags Perl, for, foreach, loop, infinite loop
4-
=indexes for, foreach, loop
5-
=status draft ready for proofreading
2+
=timestamp 2013-03-23T20:45:13
3+
=indexes for, foreach, loop, infinite loop
4+
=status show
65
=author szabgab
76
=index 1
87
=archive 1
@@ -23,11 +22,10 @@ but this construct is actually available in many programming languages.
2322
The <b>for</b> keyword in Perl can work in two different ways.
2423
It can work just as a <b>foreach</b> loop works and it can act
2524
as a 3-part C-style for loop. It is called C-style though
26-
this is employed in many languages.
25+
it is available in many languages.
2726

28-
I'll describe how this works though in Perl people usually
29-
prefer to write the <hl>foreach</hl> style loop as described
30-
in the section about <a href="http://szabgab.com/perl-arrays.html">perl arrays</a>.
27+
I'll describe how this works although I prefer to write the <hl>foreach</hl>
28+
style loop as described in the section about <a href="http://szabgab.com/perl-arrays.html">perl arrays</a>.
3129

3230
The two keywords <hl>for</hl> and <hl>foreach</hl> can be used as synonyms.
3331
Perl will work out which meaning you had in mind.

perl-arrays.tt

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
=title Perl Arrays
2+
=timestamp 2013-03-23T20:45:02
3+
=indexes @, array, arrays, length, size, foreach, Data::Dumper, scalar, push, pop, shift
4+
=status show
5+
=author szabgab
6+
=index 1
7+
=archive 1
8+
=feed 1
9+
=comments 1
10+
=social 1
11+
12+
=abstract start
13+
14+
In this episode of the <a href="/perl-tutorial">Perl Tutorial</a> we are going to learn about <b>arrays in Perl</b>.
15+
This is an overview of how arrays work in Perl. We'll see more detailed explanations later.
16+
17+
Variable names of arrays in Perl start with the at mark: <hl>@</hl>.
18+
19+
Due to our insistance on using <hl>strict</hl> you have to declare these variables using the <hl>my</hl> keyword
20+
before the first usage.
21+
22+
=abstract end
23+
24+
Remember, all the examples below assume your file starts with
25+
26+
<code lang="perl">
27+
use strict;
28+
use warnings;
29+
use 5.010;
30+
</code>
31+
32+
Declare an array:
33+
34+
<code lang="perl">
35+
my @names;
36+
</code>
37+
38+
Declare and assign values:
39+
40+
<code lang="perl">
41+
my @names = ("Foo", "Bar", "Baz");
42+
</code>
43+
44+
45+
<h2>Debugging of an array</h2>
46+
47+
<code lang="perl">
48+
use Data::Dumper qw(Dumper);
49+
50+
my @names = ("Foo", "Bar", "Baz");
51+
say Dumper \@names;
52+
</code>
53+
54+
The output is:
55+
56+
<code>
57+
$VAR1 = [
58+
'Foo',
59+
'Bar',
60+
'Baz'
61+
];
62+
</code>
63+
64+
<h2>foreach loop and perl arrays</h2>
65+
66+
<code lang="perl">
67+
my @names = ("Foo", "Bar", "Baz");
68+
foreach my $n (@names) {
69+
say $n;
70+
}
71+
</code>
72+
73+
will print:
74+
75+
<code>
76+
Foo
77+
Bar
78+
Baz
79+
</code>
80+
81+
<h2>Accessing an element of an array</h2>
82+
83+
<code lang="perl">
84+
my @names = ("Foo", "Bar", "Baz");
85+
say $names[0];
86+
</code>
87+
88+
Note, when accessing a single element of an array the leading sigil changes from <hl>@</hl> to <hl>$</hl>.
89+
This might cause confustion to some people, but if you think about it, it is quite obvious why.
90+
91+
<hl>@</hl> marks plural and <hl>$</hl> marks singular. When accessing a single element
92+
of an array it behaves just as a regular scalar variable.
93+
94+
<h2>Indexing array</h2>
95+
96+
The indexes of an array start from 0. The largest index is always in the variable called
97+
<hl>$#name_of_the_array</hl>. So
98+
99+
<code lang="perl">
100+
my @names = ("Foo", "Bar", "Baz");
101+
say $#names;
102+
</code>
103+
104+
Will print 2 because the indexes are 0,1 and 2.
105+
106+
<h2>Length or size of an array</h2>
107+
108+
In Perl there is no special function to fetch the size of an array, but there
109+
are several ways to obtain that value. For one, the size of the array is one more
110+
than the largest index. In the above case <hl>$#names+1</hl> is the <b>size</b> or
111+
<b>length</b> of the array.
112+
113+
In addition the <hl>scalar</hl> function can be used to to obtain the size of an array:
114+
115+
<code lang="perl">
116+
my @names = ("Foo", "Bar", "Baz");
117+
say scalar @names;
118+
</code>
119+
120+
Will print 3.
121+
122+
The scalar function is sort of a casting function that - among other things - converts an
123+
array to a scalar. Due to an arbitrary, but clever decision this conversion yields the size
124+
of the array.
125+
126+
<h2>Loop on the indexes of an array</h2>
127+
128+
There are cases when looping over the values of an array is not enough.
129+
We might need both the value and the index of that value.
130+
In that case we need to loop over the indexes, and obtain the values using the
131+
indexes:
132+
133+
<code lang="perl">
134+
my @names = ("Foo", "Bar", "Baz");
135+
foreach my $i (0 .. $#names) {
136+
say "$i - $names[$i]";
137+
}
138+
</code>
139+
140+
prints:
141+
142+
<code>
143+
0 - Foo
144+
1 - Bar
145+
2 - Baz
146+
</code>
147+
148+
<h2>Push on Perl array</h2>
149+
150+
<hl>push</hl> appends a new value to the end of the array, extending it:
151+
152+
<code lang="perl">
153+
my @names = ("Foo", "Bar", "Baz");
154+
push @names, 'Moo';
155+
156+
say Dumper \@names;
157+
</code>
158+
159+
The result is:
160+
161+
<code>
162+
$VAR1 = [
163+
'Foo',
164+
'Bar',
165+
'Baz',
166+
'Moo'
167+
];
168+
</code>
169+
170+
171+
<h2>Pop from Perl array</h2>
172+
173+
<hl>pop</hl> fetches the last element from the array:
174+
175+
<code lang="perl">
176+
my @names = ("Foo", "Bar", "Baz");
177+
my $last_value = pop @names;
178+
say "Last: $last_value";
179+
say Dumper \@names;
180+
</code>
181+
182+
The result is:
183+
184+
<code>
185+
Last: Baz
186+
$VAR1 = [
187+
'Foo',
188+
'Bar',
189+
];
190+
</code>
191+
192+
<h2>shift the Perl array</h2>
193+
194+
<hl>shift</hl> will return the left most element
195+
of an array and move all the other elements to the left.
196+
197+
<code lang="perl">
198+
my @names = ("Foo", "Bar", "Baz");
199+
200+
my $first_value = shift @names;
201+
say "First: $first_value";
202+
say Dumper \@names;
203+
</code>
204+
205+
The result is:
206+
207+
<code>
208+
First: Foo
209+
$VAR1 = [
210+
'Bar',
211+
'Baz',
212+
];
213+
</code>
214+

perl-tutorial.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ You'll learn how to use the CPAN and several specific CPAN modules.
2222
It will be a good foundation for you to build on.
2323

2424
The free on-line version of the tutorial is currently in development. Many parts are ready. Additional
25-
parts are being published every few days. The latest one was published on March 21, 2013.
25+
parts are being published every few days. The latest one was published on March 23, 2013.
2626
If you are interested in getting updated when new parts are published,
2727
please <a href="/register">subscribe to the newsletter</a>.
2828

@@ -107,7 +107,7 @@ to practice what you have learned.
107107
<li><a href="http://szabgab.com/for-loop-in-perl.html">The for loop in Perl</a></li>
108108
<li>Lists in Perl</li>
109109
<li>Using Modules</li>
110-
<li><a href="http://szabgab.com/perl-arrays.html">Arrays in Perl</a></li>
110+
<li><a href="/perl-arrays">Arrays in Perl</a></li>
111111
<li>Process command line parameters @ARGV, Getopt::Long</li>
112112
<li><a href="/how-to-read-a-csv-file-using-perl">How to read and process a CSV file? (split, Text::CSV_XS)</a></li>
113113
<li><a href="/join">join</a></li>

0 commit comments

Comments
 (0)