Skip to content

Commit 20402a1

Browse files
committed
initial commit
0 parents  commit 20402a1

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

dist.ini

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name = Array-Split
2+
author = Christian Walde <[email protected]>
3+
license = WTFPL_2
4+
copyright_holder = Christian Walde
5+
copyright_year = 2010
6+
7+
[AutoVersion]
8+
[PkgVersion]
9+
[GatherDir]
10+
[ExecDir]
11+
[PruneCruft]
12+
[ManifestSkip]
13+
[AutoPrereqs]
14+
[MetaYAML]
15+
[License]
16+
[Readme]
17+
[PodWeaver]
18+
[ExtraTests]
19+
[PodCoverageTests]
20+
[PodSyntaxTests]
21+
[KwaliteeTests]
22+
[CompileTests]
23+
[PerlTidy]
24+
[MetaConfig]
25+
[MetaJSON]
26+
[CheckChangeLog]
27+
[NextRelease]
28+
[MakeMaker]
29+
[Manifest]
30+
[TestRelease]
31+
[ConfirmRelease]
32+
[FakeRelease]
33+
34+
[Git::Check]
35+
[Git::Commit]
36+
[Git::Tag]
37+
[Git::Push]
38+
[Git::CommitBuild]
39+
40+
[MetaNoIndex]
41+
dir = t
42+
dir = utils
43+
44+
[Prereqs / TestRequires]
45+
Test::Simple = 0.96
46+
47+
[MetaResources]
48+
repository.web = http://github.com/wchristian/Array-Split
49+
repository.url = http://github.com/wchristian/Array-Split.git
50+
repository.type = git

lib/Array/Split.pm

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use strict;
2+
use warnings;
3+
4+
package Array::Split;
5+
6+
# ABSTRACT: split an array into sub-arrays
7+
8+
use Sub::Exporter::Simple qw( split_by split_into );
9+
use List::Util 'max';
10+
use POSIX 'ceil';
11+
12+
=head1 SYNOPSIS
13+
14+
use Array::Split qw( split_by split_into );
15+
16+
=head1 DESCRIPTION
17+
18+
This module offers functions to separate all the elements of one array into multiple arrays.
19+
20+
=head2 split_by ( $split_size, @original )
21+
22+
Splits up the original array into sub-arrays containing the contents of the original. Each sub-array's size is the same
23+
or less than $split_size, with the last one usually being the one to have less if there are not enough elements in
24+
@original.
25+
26+
=cut
27+
28+
sub split_by {
29+
my ( $split_size, @original ) = @_;
30+
31+
$split_size = ceil max( $split_size, 1 );
32+
33+
my @sub_arrays;
34+
for my $element ( @original ) {
35+
push @sub_arrays, [] if !@sub_arrays;
36+
push @sub_arrays, [] if @{ $sub_arrays[-1] } >= $split_size;
37+
38+
push @{ $sub_arrays[-1] }, $element;
39+
}
40+
41+
return @sub_arrays;
42+
}
43+
44+
=head2 split_into ( $count, @original )
45+
46+
Splits the given array into even-sized (as even as maths allow) sub-arrays. It tries to create as many sub-arrays as
47+
$count indicates, but will return less if there are not enough elements in @original.
48+
49+
Returns a list of array references.
50+
51+
=cut
52+
53+
sub split_into {
54+
my ( $count, @original ) = @_;
55+
56+
$count = max( $count, 1 );
57+
58+
my $size = ceil @original / $count;
59+
60+
return split_by( $size, @original );
61+
}
62+
63+
1;

t/basic.t

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/perl
2+
use strict;
3+
use warnings;
4+
5+
BEGIN {
6+
use Cwd;
7+
chdir '..' if getcwd =~ m@/t$@;
8+
use lib 'lib';
9+
}
10+
11+
package basic_tests;
12+
13+
use Test::Most;
14+
15+
use_ok( 'Array::Split', qw( split_by split_into ) );
16+
17+
my @by_res = split_by( 2, ( 1, 2, 3, 4, 5 ) );
18+
is_deeply( \@by_res, [ [1, 2], [3, 4], [5] ] );
19+
20+
my @into_res = split_into( 2, ( 1, 2, 3, 4, 5 ) );
21+
is_deeply( \@into_res, [ [1, 2, 3], [4, 5] ] );
22+
23+
done_testing();
24+
25+
exit;

0 commit comments

Comments
 (0)