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

Re: Range idea

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
January 23, 2014 16:47
Subject:
Re: Range idea
Message ID:
20140123164741.5614d5b5@shy.leonerd.org.uk
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

  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

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

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