Perl 6: Arrays with unique values

Note

This article is about Perl 6. If you are looking for solution regarding the current production version of Perl 5, please check out the Perl 5 tutorial. Specifially there is an article how to handle Unique values in an array in Perl 5.


The first entry of the Perl 6 Tricks and Treats newsletter. Here you can read about how to get rid of duplicate values in an array.


This entry was first sent out as part of the Perl 6 Tricks and Treats. Visit here to subscribe.

Welcome to the Perl 6 Tricks and Treats

I opened the list on 11th March 2009 and this was the first message and the first post.

As of the name of this newsletter, I was not sure how I'd like to call it. I started with "Perl 6 code" and then other ideas such as "bread crumbs" and "bait" came up. At one point I was even thinking about "Gragger" - as I opened the list on the day of Purim - but then my sister suggested "treats" and then we arrived to the expression Trick or treat

I loved the idea but I though in our case tricks are treats so we should use "and" between the two words.

Anyway, tips would be also a good name but I did not want to call it "Perl 6 Tips" as there already is a Perl Tips list and a good one. It's worth to subscribe or read them on their web site.

This brings me to one of the things I can do on this list. I can go over the various tips given by the folks at Perl Training Australia and write the Perl 6 version for some of them.

Arrays with unique values

I'll start with the most recent tip from Jacinta and Paul:

Tricks with Arrays and Hashes

Basically they show various ways how one can take a list of values and return a sublist of the same values after eliminating the duplicates.

With Perl 6 its much easier to solve the problem as there is a built-in called "uniq" that will do the job.


    use v6;

    my @duplicates = (1, 1, 2, 5, 1, 4, 3, 2, 1);
    say @duplicates.perl;           # [1, 1, 2, 5, 1, 4, 3, 2, 1]


    my @unique = uniq @duplicates;
    say @unique.perl;               # prints [1, 2, 5, 4, 3]

This works on strings as well:


    use v6;

    my @chars = <b c a d b a a a b>;
    say @chars.perl;                # ["b", "c", "a", "d", "b", "a", "a", "a", "b"]

    my @singles = uniq @chars;
    say @singles.perl;              # ["b", "c", "a", "d"]


Perl 6 Comments

A few comments to people new to Perl 6:

One should start every Perl 6 script by asking for v6; version 6 of Perl.

You can add .perl to every kind of variable and get back a dumped representation of the thing. In our case the thing is an array. Very handy for debugging.

say() is a built-in function in Perl 6 and it behaves as say() in Perl 5.10 printing to the screen and adding a newline at the end.

Angle brackets < > allow one to create a list of values from a space separated list of items. In Perl 5 the qw() operator was used for this.

Method calls

Actually "uniq" can also be used as a method call on the array object...


    use v6;

    my @chars = <b c a d b a a a b>;
    
    my @singles = @chars.uniq;
    

... and some people will prefer to add the .say call to after the .perl call:


    @singles.perl.say;              # ["b", "c", "a", "d"]    

How to get Perl 6 ?

The developers keep an entry with up to date instructions on how to get Rakudo.


This entry was first sent out as part of the Perl 6 Tricks and Treats. Visit here to subscribe.


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


Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed