Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Mention how to reference last element of an array
  • Loading branch information
Paul Cochrane committed Feb 26, 2015
1 parent ee49c3c commit 3b6d55b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/Language/traps.pod
Expand Up @@ -186,6 +186,21 @@ Note that this is I<also> the behaviour in Perl 5:
perl -E "say scalar(my @array = [])" #=> 1
perl -E "say scalar(my @array = ())" #=> 0
=head2 Referencing the last element of an array
In Perl 5 one could reference the last element of an array by asking for the
"-1th" element of the array, e.g.:
my @array = qw{victor alice bob charlie eve};
say @array[-1]; #=> eve
In Perl 6 it is not possible to use negative subscripts, however the same is
achieved by actually using a function, namely C<*-1>. Thus accessing the
last element of an array becomes:
my @array = qw{victor alice bob charlie eve};
say @array[*-1]; #=> eve
=end pod

# vim: expandtab shiftwidth=4 ft=perl6

0 comments on commit 3b6d55b

Please sign in to comment.