Arrays & lists in Perl
The @ sigil for the whole array, $ for one element, negative indexes for free.
use strict;
use warnings;
my @langs = ("perl", "ruby", "go");
push @langs, "rust";
# a single element wears the scalar sigil
print "first: $langs[0], last: $langs[-1]\n";
print scalar(@langs), " languages\n";
my @pair = @langs[1, 2];
print "slice: @pair\n";
print join(", ", sort @langs), "\n";
How it works
pushappends;$langs[0]and$langs[-1]grab the ends with the scalar sigil.scalar(@langs)is the count;@langs[1, 2]slices two elements at once.sortreturns a fresh ordered list andjoinglues it into one string.
Keywords and builtins used here
joinmyprintpushscalarsortuse
The run, in numbers
- Lines
- 13
- Characters to type
- 297
- Tokens
- 64
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 55 seconds.
Step 2 of 3 in Scalars & sigils, step 2 of 27 in Language basics.