Skip to content

Commit 3b6d55b

Browse files
committed
Mention how to reference last element of an array
1 parent ee49c3c commit 3b6d55b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/Language/traps.pod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,21 @@ Note that this is I<also> the behaviour in Perl 5:
186186
perl -E "say scalar(my @array = [])" #=> 1
187187
perl -E "say scalar(my @array = ())" #=> 0
188188
189+
=head2 Referencing the last element of an array
190+
191+
In Perl 5 one could reference the last element of an array by asking for the
192+
"-1th" element of the array, e.g.:
193+
194+
my @array = qw{victor alice bob charlie eve};
195+
say @array[-1]; #=> eve
196+
197+
In Perl 6 it is not possible to use negative subscripts, however the same is
198+
achieved by actually using a function, namely C<*-1>. Thus accessing the
199+
last element of an array becomes:
200+
201+
my @array = qw{victor alice bob charlie eve};
202+
say @array[*-1]; #=> eve
203+
189204
=end pod
190205

191206
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)