文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Perl 6: Arrays with unique values

Perl 6: Arrays with unique values

时间:2009-04-29  来源:cobrawgl

Abstract

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.

相关阅读 更多 +
排行榜 更多 +
打螺丝高手

打螺丝高手

模拟经营 下载
解救火柴人计划安卓版

解救火柴人计划安卓版

体育竞技 下载
鸡生化精英安卓版

鸡生化精英安卓版

飞行射击 下载