Array::Windowed - return a windowed slice of the array
use Array::Windowed qw(windowed_array); # from an array my $new_array = array_window @old_array, $start_index, $count); # from an arrayref my $new_array2 = &array_window($array_ref, $start_index, $count);
Simple module to return a slice of the passed array as specified by a start position and a number of elements. Unlike the built in slicing functions this does not populate the returned array with undefs for out of bounds operations, but simply returns a smaller array.
undef
Exported on demand (or you can call it fully qualified)
Returns an array reference containing up to $count elements from the @old_array starting at $start_index.
$count
@old_array
$start_index
Unlike a traditional array slice elements outside the bounds of the array are simply dropped. Negative indexes are treated as normal indexes that occur before the 0th index (rather than indexes counting backwards from the start of the array.)
This is probably best shown with a series of examples:
my @array = ['a'..'z']; # ["a".."z"] array_windowed(@array, 0, 26); # ["a","b","c","d","e"] array_windowed(@array, 0, 5); # ["b","c","d","e","f"] array_windowed(@array, 1, 5); # [] array_windowed(@array, 26, 5); # [] array_windowed(@array, -50, 5); # ["x","y","z"] array_windowed(@array, 23, 5); # ["a","b","c"] array_windowed(@array, -2, 5); # ["a".."z"] &array_windowed(\@array, 0, 26);
Why this module?
Because the edge cases of indexes outside the bounds of the array don't do the same thing (they return lots of undefs mainly)
It's destructive on the original array.
Because it tries too hard to DTRT, which isn't the RT for us. For example, if you have a count bigger than the entire array then it always returns the entire array irregardless of the start position.
This doesn't mean it's not the module for you, however.
This concept of windowing we're using doesn't use pages, but rather a start position and a count.
Written by Mark Fowler <mark@twoshortplanks.com>
Copyright Mark Fowler 2012. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This module does no sanity checking on the values that are passed into it (i.e. it does not check that $start or $count are integer numbers, nor does it check that the first argument is an array reference if you disable prototyping.)
$start
Bugs should be reported via this distribution's CPAN RT queue. This can be found at https://rt.cpan.org/Dist/Display.html?Array-Windowed
You can also address issues by forking this distribution on github and sending pull requests. It can be found at http://github.com/2shortplanks/Array-Windowed
Array::Window, Data::Page
To install Array::Windowed, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Array::Windowed
CPAN shell
perl -MCPAN -e shell install Array::Windowed
For more information on module installation, please visit the detailed CPAN module installation guide.