|
| 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 | + |
0 commit comments