How to get rid of duplicate values in an array in Perl 6?
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 quite easy to eliminate duplicate values from a list 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; # Array.new(1, 1, 2, 5, 1, 4, 3, 2, 1) my @unique = uniq @duplicates; say @unique.perl; # Array.new(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; # Array.new("b", "c", "a", "d", "b", "a", "a", "a", "b") my @singles = uniq @chars; say @singles.perl; # Array.new("b", "c", "a", "d")
A few comments to people new to Perl 6:
One should start every Perl 6 script by asking for v6; version 6 of Perl. It is important in order to avoid strange error messages when you happen to run it with perl 5.
You can add .perl to every kind of variable and get back a dumped representation of the data in it. 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.
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; say @singles.perl; # Array.new("b", "c", "a", "d")
... and some people will prefer to add the .say method call at the end of the expression:
though I think this isn't as clear as having the say at the beginning.
The developers keep an entry with up to date instructions on how to get Rakudo.
Published on 2012-07-04