Chinaunix首页 | 论坛 | 博客
  • 博客访问: 573325
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类:

2009-04-29 14:28:47

Abstract

The first entry of the 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 . Visit .

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

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 list and a good one. It's worth to 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 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:

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 = ;
    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 = ;
    
    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 .

阅读(858) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~