Camelia

Perl 5 to Perl 6 - Arrays

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.

Note! This site is about Perl 6, the future version of Perl.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.

Creating arrays, Data::Dumper, debug prints

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)

quote word qw() is no longer used

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")

Array interpolation in strings

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.

Elements of an array, sigil invariance

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

Interpolation of an array element

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

Number of elements in an array

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

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")

for and foreach loop

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.

Iterating over the indexes of an array

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

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

Conclusion

That's it for today. There are a lot more things to say about arrays, but this is probably enough for one reading.


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-08-14



Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: