Skrevet av Emne: How to get a slice of an array in Perl  (Lest 4260 ganger)

ATC

  • Gjest
How to get a slice of an array in Perl
« på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • 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.



    ATC

    • Gjest
    [Solved] How to get a slice of an array in Perl
    « Svar #1 på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • 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);
    }