Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ensembl
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ensembl-gh-mirror
ensembl
Commits
c6eb06d6
Commit
c6eb06d6
authored
12 years ago
by
Andy Yates
Browse files
Options
Downloads
Patches
Plain Diff
Adding an array splitter
parent
6356b422
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/Bio/EnsEMBL/Utils/Scalar.pm
+29
-1
29 additions, 1 deletion
modules/Bio/EnsEMBL/Utils/Scalar.pm
modules/t/utilsScalar.t
+10
-0
10 additions, 0 deletions
modules/t/utilsScalar.t
with
39 additions
and
1 deletion
modules/Bio/EnsEMBL/Utils/Scalar.pm
+
29
−
1
View file @
c6eb06d6
...
...
@@ -106,11 +106,12 @@ our @EXPORT_OK;
assert_ref assert_ref_can assert_numeric assert_integer assert_boolean assert_strand assert_file_handle
wrap_array
scope_guard
split_array
)
;
%EXPORT_TAGS
=
(
assert
=>
[
qw(assert_ref assert_ref_can assert_integer assert_numeric assert_boolean assert_strand assert_file_handle)
],
check
=>
[
qw(check_ref check_ref_can)
],
array
=>
[
qw/wrap_array/
],
array
=>
[
qw/wrap_array
split_array
/
],
all
=>
[
@EXPORT_OK
]
);
...
...
@@ -416,6 +417,33 @@ sub assert_file_handle {
return
1
;
}
=head2 split_array
Arg [1] : Integer Maximum size of an array produced
Arg [2] : ArrayRef The array to split
Description : Takes an array of values and splits the array into multiple
arrays where the maximum size of each array is as specified
Example :
Returntype : ArrayRef of ArrayRefs where each element is a split list
=cut
sub
split_array
{
my
(
$amount
,
$array
)
=
@_
;
assert_ref
(
$array
,
'
ARRAY
',
'
array
');
my
@split
;
my
$counter
=
0
;
my
$index
=
0
;
foreach
my
$e
(
@$array
)
{
if
(
$counter
==
$amount
)
{
$index
++
;
$counter
=
0
;
}
push
(
@
{
$split
[
$index
]},
$e
);
$counter
++
;
}
return
\
@split
;
}
=head2 scope_guard
Arg [1] : CodeRef The block of code to exit once it escapes out of scope
...
...
This diff is collapsed.
Click to expand it.
modules/t/utilsScalar.t
+
10
−
0
View file @
c6eb06d6
...
...
@@ -159,4 +159,14 @@ close($_) for ($scalar_fh, $other_scalar_fh);
is
(
$v
,
'
wibble
',
'
Value has been reset even after a die
');
}
#Array split
{
my
$original_array
=
[
1
..
7
];
my
$split_two
=
split_array
(
2
,
$original_array
);
is_deeply
(
$split_two
,
[[
1
,
2
],[
3
,
4
],[
5
,
6
],[
7
]],
'
Checking split of 7 element array into arrays of max size 2
')
or
diag
explain
$split_two
;
my
$split_ten
=
split_array
(
10
,
$original_array
);
is_deeply
(
$split_ten
,
[
$original_array
],
'
Checking split of 7 element array into arrays of max size 10
')
or
diag
explain
$split_ten
;
dies_ok
{
split_array
(
1
,
{})
}
'
Passing in a non-array ref means death
';
}
done_testing
();
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment