develooper Front page | perl.perl5.porters | Postings from January 2014

Re: Range idea

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
January 23, 2014 17:11
Subject:
Re: Range idea
Message ID:
20140123181149.263c8d6f@pc09.procura.nl
On Thu, 23 Jan 2014 16:47:41 +0000, "Paul \"LeoNerd\" Evans"
<leonerd@leonerd.org.uk> wrote:

> On Thu, 23 Jan 2014 17:32:53 +0100
> Aristotle Pagaltzis <pagaltzis@gmx.de> wrote:
> 
> > Why not simply add a weak keyword and avoid the entire syntax design
> > exercise?
> > 
> >    qw( a b c d )[2 .. lastindex]
> 
> That doesn't solve the problem any more than any of the others do.
> 
> What would this do?
> 
>   my @range = 2 .. lastindex;
>   my @letters = ('A' .. 'Z');
> 
>   say @letters[@range];
> 
> ?
> 
> Ultimately, 'lastindex' is going to fail however this works.
> 
> Why not just

Because that copies and is thus expensive

>   sub slice
>   {
>      my ( $first, $last, @items ) = @_;
>      $last += @items if $last < 0;
>      return @items[$first..$last];
>   }
> 
>   say slice 2, -1, 'A' .. 'Z'
> 
> This justworks.
> 
> If you want, I could pop that in List::Util right now, have it on CPAN
> within the hour, available for all perls back to 5.8

That would be useful anyway, I guess

For what I've read in the feedback, the new use of $# looks DWIMisher
than all other options, and $# already had magic.

-- 
H.Merijn Brand  http://tux.nl   Perl Monger  http://amsterdam.pm.org/
using perl5.00307 .. 5.19   porting perl5 on HP-UX, AIX, and openSUSE
http://mirrors.develooper.com/hpux/        http://www.test-smoke.org/
http://qa.perl.org   http://www.goldmark.org/jeff/stupid-disclaimers/

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