develooper Front page | perl.perl6.users | Postings from August 2019

Subscripting with a list of slices

Thread Next
From:
Sean McAfee
Date:
August 16, 2019 10:18
Subject:
Subscripting with a list of slices
Message ID:
CANan03ZuUKtemSWiQ7VJHNcXi9-aRZujGkFRsuOuSgeFfDceLQ@mail.gmail.com
Today I was surprised and pleased to find that I could apparently subscript
an array with a list of lists of indices, and get a list of slices back:

> my @array = 9 ... 0
[9 8 7 6 5 4 3 2 1 0]
> @array[map { $_, $_ + 1 }, ^9]
((9 8) (8 7) (7 6) (6 5) (5 4) (4 3) (3 2) (2 1) (1 0))

Neat!  But when I tried to clean up my code a bit, it broke.

> my @index = map { $_, $_ + 1 }, ^9
[(0 1) (1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8) (8 9)]
> @array[@index]
(7 7 7 7 7 7 7 7 7)

Weird.  But map returns a Seq, right?  So maybe...

> @array[@index.Seq]
(7 7 7 7 7 7 7 7 7)

Hmm.  How about:

> @array[@index.List]
((9 8) (8 7) (7 6) (6 5) (5 4) (4 3) (3 2) (2 1) (1 0))

That works again.  But why?  Is this the intended behavior?  If so, is it
documented somwhere?

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About