October 2015 Archives

List::Slice - Slice-like Operations for Lists

How many times have you needed to do this?

my @found_names = grep { /^[A-D]/ } @all_names;
my @topfive = @found_names[0..4];

Or worse, this.

my @topfive = ( grep { /^[A-D]/ } @all_names )[0..4];

There's got to be a better way!

Or this.

my @bottomfive = @names < 5 ? @names : @names[$#names-5..$#names];

Or this.

my @names
        = map { $_->[0] }
        sort { $a->[1] <=> $b->[1] }
        grep { $_->[1] > $now }
        map { [ $_->{name}, parse_date( $_->{birthday} ) ] }
        @all_users;
my @topfive = @names[0..4];

There's got to be a better way!

There's got to be a better way!

Now there is! Introducing: List::Slice!

About preaction

user-pic