develooper Front page | perl.perl5.porters | Postings from August 2021

Re: idea: everything-slices

Thread Previous | Thread Next
From:
Martijn Lievaart
Date:
August 20, 2021 06:18
Subject:
Re: idea: everything-slices
Message ID:
18f42c4f-17e1-36c0-5dae-6177c75a90ab@rtij.nl

Op 19-08-2021 om 10:17 schreef Ed Avis:
> A new slice syntax should look closely at what Python does.  The start 
> and end are separated by a colon and the range is half-closed, so 
> "start up until end", as commonly used in C-style for-loops and C++ 
> iterators.  Either can be omitted and so an "everything slice" is list[:].
>
> I think the half-closed range is more elegant than Perl's .. giving a 
> fully inclusive range, which then requires the $#a syntax to give one 
> less than the length of @a.  In Perl I guess it would look something like
>
> @array[:3]        # equivalent to @array[0 .. 2]
> @array[0:2]      # equivalent to @array[0, 1]
> @array[:$len]  # equivalent to @array[0 .. $len - 1]
> @array[:]          # equivalent to @array[0 .. $#array]


How about reusing .. for that?


@array[..2]        # equivalent to @array[0 .. 2]
@array[0..1]      # equivalent to @array[0, 1] (this already works today)
@array[..$len-1]  # equivalent to @array[0 .. $len - 1]
@array[..]          # equivalent to @array[0 .. $#array]

AFAICS these are currently syntax errors.

HTH,

M4





Thread Previous | 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