Ano-Tech Computers
Enter keyword:

How to get a slice of an array in Perl
Problem:
The Perl language includes the splice() function, which upon returning a subset of records from an array (a slice) removes those records from the original array. But it does not include a slice() function. "Programming Perl" describes a slow way to do it.
 
Solution:
Here's a faster one, although it does use more memory:

sub slice
{
my (@array) = @_;
my $count = pop @array;
my $offset = pop @array;
return splice(@array, $offset, $count);
}

 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles