typestar

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

  1. push appends; $langs[0] and $langs[-1] grab the ends with the scalar sigil.
  2. scalar(@langs) is the count; @langs[1, 2] slices two elements at once.
  3. sort returns a fresh ordered list and join glues it into one string.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Scalars & sigils, step 2 of 27 in Language basics.

← Previous Next →