List::MoreUtils - Provide the stuff missing in List::Util
use List::MoreUtils qw(any all none notall true false firstidx lastidx insert_after insert_after_string apply after after_incl before before_incl indexes lastval firstval pairwise each_array natatime mesh);
List::MoreUtils provides some trivial but commonly needed functionality on lists which is not going to go into List::Util.
List::MoreUtils
List::Util
All of the below functions are implementable in only a couple of lines of Perl code. Using the functions from this module however should give slightly better performance as everything is implemented in C. The pure-Perl implementation of these functions only serves as a fallback in case the C portions of this module couldn't be compiled on this machine.
Returns a true value if any item in LIST meets the criterion given through BLOCK. Sets $_ for each item in LIST in turn:
$_
print "At least one value undefined" if any { !defined($_) } @list;
Returns false otherwise, or undef if LIST is empty.
undef
Returns a true value if all items in LIST meet the criterion given through BLOCK. Sets $_ for each item in LIST in turn:
print "All items defined" if all { defined($_) } @list;
Logically the negation of any. Returns a true value if no item in LIST meets the criterion given through BLOCK. Sets $_ for each item in LIST in turn:
any
print "No value defined" if none { defined($_) } @list;
Logically the negation of all. Returns a true value if not all items in LIST meet the criterion given through BLOCK. Sets $_ for each item in LIST in turn:
all
print "Not all values defined" if notall { defined($_) } @list;
Counts the number of elements in LIST for which the criterion in BLOCK is true. Sets $_ for each item in LIST in turn:
printf "%i item(s) are defined", true { defined($_) } @list;
Counts the number of elements in LIST for which the criterion in BLOCK is false. Sets $_ for each item in LIST in turn:
printf "%i item(s) are not defined", false { defined($_) } @list;
Returns the index of the first element in LIST for which the criterion in BLOCK is true. Sets $_ for each item in LIST in turn:
my @list = (1, 4, 3, 2, 4, 6); printf "item with index %i in list is 4", firstidx { $_ == 4 } @list; __END__ item with index 1 in list is 4
Returns -1 if no such item could be found.
-1
first_index is an alias for firstidx.
first_index
firstidx
Returns the index of the last element in LIST for which the criterion in BLOCK is true. Sets $_ for each item in LIST in turn:
my @list = (1, 4, 3, 2, 4, 6); printf "item with index %i in list is 4", lastidx { $_ == 4 } @list; __END__ item with index 4 in list is 4
last_index is an alias for lastidx.
last_index
lastidx
Inserts VALUE after the first item in LIST for which the criterion in BLOCK is true. Sets $_ for each item in LIST in turn.
my @list = qw/This is a list/; insert_after { $_ eq "a" } "longer" => @list; print "@list"; __END__ This is a longer list
Inserts VALUE after the first item in LIST which is equal to STRING.
my @list = qw/This is a list/; insert_after_string "a", "longer" => @list; print "@list"; __END__ This is a longer list
Applies BLOCK to each item in LIST and returns a list of the values after BLOCK has been applied. In scalar context, the last element is returned. This function is similar to map but will not modify the elements of the input list:
map
my @list = (1 .. 4); my @mult = apply { $_ *= 2 } @list; print "\@list = @list\n"; print "\@mult = @mult\n"; __END__ @list = 1 2 3 4 @mult = 2 4 6 8
Think of it as syntactic sugar for
for (my @mult = @list) { $_ *= 2 }
Returns a list of the values of LIST after (and not including) the point where CODE returns a true value. Sets $_ for each element in LIST in turn.
@x = after { $_ % 5 == 0 } (1..9); # returns 6, 7, 8, 9
Same as after but also inclues the element for which BLOCK is true.
after
Returns a list of values of LIST upto (and not including) the point where CODE returns a true value. Sets $_ for each element in LIST in turn.
Same as before but also includes the element for which BLOCK is true.
before
Evaluates BLOCK for each element in LIST (assigned to $_) and returns a list of the indices of those elements for which BLOCK returned a true value. This is just like grep only that it returns indices instead of values:
grep
@x = indexes { $_ % 2 == 0 } (1..10); # returns 1, 3, 5, 7, 9
Returns the first element in LIST for which BLOCK evaluates to true. Each element of LIST is set to $_ in turn. Returns undef if no such element has been found.
first_val is an alias for firstval.
first_val
firstval
Returns the last value in LIST for which BLOCK evaluates to true. Each element of LIST is set to $_ in turn. Returns undef if no such element has been found.
last_val is an alias for lastval.
last_val
lastval
Evaluates BLOCK for each pair of elements in ARRAY1 and ARRAY2 and returns a new list consisting of BLOCK's return values. The two elements are set to $a and $b. Note that those two are aliases to the original value so changing them will modify the input arrays.
$a
$b
@a = (1 .. 5); @b = (10 .. 15); @x = pairwise { $a + $b } @a, @b; # returns 11, 13, 15, 17, 20 # mesh with pairwise @a = qw/a b c/; @b = qw/1 2 3/; @x = pairwise { ($a, $b) } @a, @b; # returns a, 1, b, 2, c, 3
Creates an array iterator to return the elements of the list of arrays ARRAY1, ARRAY2 throughout ARRAYn in turn. That is, the first time it is called, it returns the first element of each array. The next time, it returns the second elements. And so on, until all elements are exhausted.
This is useful for looping over more than one array at once:
my $ea = each_array(@a, @b, @c); while ( ($a,$b,$c) = $ea->() ) { .... }
The iterator returns the empty list when it reached the end of all arrays.
If the iterator is passed an argument of 'index', then it retuns the index of the last fetched set of values, as a scalar.
index
Creates an array iterator, for looping over an array in chunks of $n items at a time. (n at a time, get it?). An example is probably a better explanation than I could give in words.
$n
Example:
my @x = ('a' .. 'g'); my $it = natatime 3, @x; while (my @vals = $it->()) { print "@vals\n"; }
This prints
a b c d e f g
Returns a list consisting of the first elements of each array, then the second, then the third, etc, until all arrays are exhausted.
Examples:
@x = qw/a b c d/; @y = qw/1 2 3 4/; @z = mesh @x, @y; # returns a, 1, b, 2, c, 3, d, 4 @a = ('x'); @b = ('1', '2'); @c = qw/zip zap zot/; @d = mesh @a, @b, @c; # x, 1, zip, undef, 2, zap, undef, undef, zot
zip is an alias for mesh.
zip
mesh
Nothing by default. To import all of this module's symbols, do the conventional
use List::MoreUtils qw/:all/;
It may make more sense though to only import the stuff your program actually needs:
use List::MoreUtils qw/any firstidx/;
This is version 0.07.
No known ones.
If you have a functionality that you could imagine being in this module, please drop me a line. This module's policy will be less strict than List::Util's when it comes to additions as it isn't a core module.
Credits go to a number of people: Steve Purkis for giving me namespace advice and James Keenan and Terrence Branno for their effort of keeping the CPAN tidier by making List::Utils obsolete.
Brian McCauley suggested the includsion of apply and provided the pure-Perl implementation for it.
apply
Eric J. Roode asked me to add all functions from his module List::MoreUtil into this one. With minor modifications, the pure-Perl implementations of those are by him.
List::MoreUtil
Tassilo von Parseval, <tassilo.von.parseval@rwth-aachen.de>
Copyright (C) 2004 by Tassilo von Parseval
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.
To install List::MoreUtils, copy and paste the appropriate command in to your terminal.
cpanm
cpanm List::MoreUtils
CPAN shell
perl -MCPAN -e shell install List::MoreUtils
For more information on module installation, please visit the detailed CPAN module installation guide.