In the previous article we looked at scalars varaiables and some scalar functions, and how they changed from Perl 5 to Perl 6.
Remember there is a lot more you can do with scalars, I just listed the things that seemed to be the most important in Perl 5 and how do they translate to Perl 6!
This time, we are going to look at arrays.
Creating an array in Perl 6 can be the same as in Perl 5 For debugging prints one will use the .perl method instead of some Data::Dumper.
use v6;
my @numbers = ("one", "two", "three");
say @numbers.perl; # Array.new("one", "two", "three")
In Perl 6, there is no need for the parentheses around lists:
use v6; my @digits = 1, 3, 6; say @digits.perl; # Array.new(1, 3, 6)
The Perl 5 qw() operator is replaced by the angle brackets:
use v6;
my @names = <foo bar baz>;
say @names.perl; # Array.new("foo", "bar", "baz")
Arrays do NOT interpolate any more in double quoted strings:
use v6; my @names = <foo bar baz>; say "@names"; # @names
You can safely write the following without escaping the at mark:
use v6; my @names = <foo bar baz>; say "joe@names.org"; # joe@names.org
If you do want to interpolate the values of an array you have to put it inside curly braces:
use v6;
my @names = <foo bar baz>;
say "names: {@names}"; # names: foo bar baz
You can read about more interesting ways to interpolate arrays.
When accessing elements of an array in Perl 6, the sigil does NOT change! This will be very strange for Perl 5 programmers, but it has some advantages in the long run.
use v6; my @names = <foo bar baz>; say @names[0]; # foo
As a more special case, an array element can be interpolated within a double-quoted string without and curly braces. The term post-circumfix is the generic name for square-brackets or curly-braces after a variable. In general, variables with post-circumfix can be be interpolated.
use v6; my @names = <foo bar baz>; say "name: @names[0]"; # name: foo
In Perl 6, ther recommended way to fetch the number of elements in an array, is to use the elems() method and the corresponding function. Actually I think the object oriented writing is much nicer here:
use v6; my @names = <foo bar baz>; say elems @names; # 3 say @names.elems; # 3
Ranges are available in Perl 6 just as in Perl 5:
use v6; my @d = 1..11; say @d.perl; # Array.new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
The same works with scalar variables on either side:
use v6; my $start = 1; my $end = 11; my @d = $start .. $end; say @d.perl; # Array.new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
A probably even more interesting aspect of ranges is that you can tell them to exclude the end-points by using a caret ^ on either, or both, ends of the range:
use v6; my $start = 1; my $end = 11; my @d = $start ^..^ $end; say @d.perl; # Array.new(2, 3, 4, 5, 6, 7, 8, 9, 10)
The range operator works with characters as well
use v6;
my @chars = ('a' .. 'd');
say @chars.perl; # Array.new("a", "b", "c", "d")
The C-style version of the Perl 5 for loop is now called loop, but I won't show it here as it is way better to use the the iterator style for loop that most people in Perl 5 write as foreach.
In Perl 6, it is spelled as for and looks like this:
use v6;
for 1..3 -> $i {
say $i;
}
The output is
1 2 3
The same works on arrays as well:
use v6;
my @names = <foo bar baz>;
for @names -> $n {
say $n;
}
Output:
foo bar baz
BTW, this is one of the cases where you don't need to declare a variable using my. The looping variable is automatically declared for you and scoped to the block of the for loop.
If you need to iterate over the indexes of an array then you could use the range from 0 to the the highest index. That is one less than the number of elements in the array. You could write @names.elems -1 as the right end of the range, or you can tell the range to exclude the end value, using the caret:
use v6;
my @names = <foo bar baz>;
for 0 ..^ @names.elems -> $i {
say "$i {@names[$i]}";
}
The output:
0 foo 1 bar 2 baz
Alternatively, you can pick a suggestions from Moritz Lenz:
use v6;
my @names = <foo bar baz>;
for @names.keys -> $i {
say "$i {@names[$i]}";
}
The keys() method borrowed from the hashes will return all the indexes of an array. Even if your array has "holes" in it, elements with undef as value, keys() will still include the indexes of such elements.
split() acts similar to its Perl 5 counterpart, but the defaults do not apply any more, and you should read the spec anyway.
use v6;
say "a,b,c".split(',').perl; # ("a", "b", "c").list
That's it for today. There are a lot more things to say about arrays, but this is probably enough for one reading.

Published on 2012-08-14