Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Slicing conflict

9 views
Skip to first unread message

Luke Palmer

unread,
Apr 12, 2005, 2:30:42 AM4/12/05
to Language List
In Perl 5:

my @a = (1,2,3);
my @b = @a[0..3];
print scalar(@b); # 4

But in Perl 6:

my @a = (1,2,3,4);
my @b = @a[1...]; # elements from 1 onward
say +@b; # should probably be 3, but with Perl 5 semantics is Inf

We have to break one of these. I think the former is the one to break,
even though that might cause some unexpected surprises here and there.
Any ideas?

Luke

Autrijus Tang

unread,
Apr 12, 2005, 2:50:26 AM4/12/05
to Luke Palmer, Language List
On Tue, Apr 12, 2005 at 12:30:42AM -0600, Luke Palmer wrote:
> But in Perl 6:
>
> my @a = (1,2,3,4);
> my @b = @a[1...]; # elements from 1 onward
> say +@b; # should probably be 3, but with Perl 5 semantics is Inf

In Pugs (r1847), after the IType refactoring, I have allowed
infinite slicing of arrays to simply mean "slice until the end".

That is:

@b = @a[1...]; # elements from 1 onward

@b = @a[1..Inf]; # same thing

I think that's far more more intuitive and convenient.

Thanks,
/Autrijus/

Brent 'Dax' Royal-Gordon

unread,
Apr 12, 2005, 3:08:43 AM4/12/05
to Luke Palmer, Language List

I was thinking about this today, actually, because my CS textbook was
talking about multidimensional arrays. If we make an infinite index
mean "slice until you can slice no more", then we can possibly have a
C<term:<*>> which is the same as C<0...>. That means we can slice
like this:

@foo[1,3; *; 7]

Which I rather like. (Although term:<*> might conflict with
prefix:<*>...hmm. I'm not sure it would in common usage--the only
things I can think of that could follow are a dot, opening or closing
bracket, semicolon or comma, or hyperoperator, none of which are
ambiguous if we stick to the no-whitespace-before-postcircumfix rule.)

By the way, this also shortens the common idiom:

@foo[3..@foo.end]

To simply:

@foo[3...]

Which strikes me as a win.

--
Brent 'Dax' Royal-Gordon <br...@brentdax.com>
Perl and Parrot hacker

"I used to have a life, but I liked mail-reading so much better."

Larry Wall

unread,
Apr 12, 2005, 7:22:07 AM4/12/05
to Language List
On Tue, Apr 12, 2005 at 12:08:43AM -0700, Brent 'Dax' Royal-Gordon wrote:
: I was thinking about this today, actually, because my CS textbook was

: talking about multidimensional arrays. If we make an infinite index
: mean "slice until you can slice no more", then we can possibly have a
: C<term:<*>> which is the same as C<0...>. That means we can slice
: like this:
:
: @foo[1,3; *; 7]
:
: Which I rather like.

Me too. Unless my memory is failing me, I believe that's what S09
already specifies.

Larry

Brent 'Dax' Royal-Gordon

unread,
Apr 12, 2005, 3:02:57 PM4/12/05
to Language List

It does include a C<term:<*>> (d'oh, should've checked), but it
doesn't specify how it works.

0 new messages