16fb12b70Safresh1# Copyright (c) 1997-2009 Graham Barr <gbarr@pobox.com>. All rights reserved. 26fb12b70Safresh1# This program is free software; you can redistribute it and/or 36fb12b70Safresh1# modify it under the same terms as Perl itself. 46fb12b70Safresh1# 56fb12b70Safresh1# Maintained since 2013 by Paul Evans <leonerd@leonerd.org.uk> 66fb12b70Safresh1 76fb12b70Safresh1package List::Util; 86fb12b70Safresh1 96fb12b70Safresh1use strict; 109f11ffb7Safresh1use warnings; 116fb12b70Safresh1require Exporter; 126fb12b70Safresh1 136fb12b70Safresh1our @ISA = qw(Exporter); 146fb12b70Safresh1our @EXPORT_OK = qw( 1556d68f1eSafresh1 all any first min max minstr maxstr none notall product reduce reductions sum sum0 16eac174f2Safresh1 sample shuffle uniq uniqint uniqnum uniqstr zip zip_longest zip_shortest mesh mesh_longest mesh_shortest 179f11ffb7Safresh1 head tail pairs unpairs pairkeys pairvalues pairmap pairgrep pairfirst 186fb12b70Safresh1); 19*e0680481Safresh1our $VERSION = "1.63"; 206fb12b70Safresh1our $XS_VERSION = $VERSION; 2156d68f1eSafresh1$VERSION =~ tr/_//d; 226fb12b70Safresh1 236fb12b70Safresh1require XSLoader; 246fb12b70Safresh1XSLoader::load('List::Util', $XS_VERSION); 256fb12b70Safresh1 2656d68f1eSafresh1# Used by shuffle() 2756d68f1eSafresh1our $RAND; 2856d68f1eSafresh1 296fb12b70Safresh1sub import 306fb12b70Safresh1{ 316fb12b70Safresh1 my $pkg = caller; 326fb12b70Safresh1 336fb12b70Safresh1 # (RT88848) Touch the caller's $a and $b, to avoid the warning of 346fb12b70Safresh1 # Name "main::a" used only once: possible typo" warning 356fb12b70Safresh1 no strict 'refs'; 366fb12b70Safresh1 ${"${pkg}::a"} = ${"${pkg}::a"}; 376fb12b70Safresh1 ${"${pkg}::b"} = ${"${pkg}::b"}; 386fb12b70Safresh1 396fb12b70Safresh1 goto &Exporter::import; 406fb12b70Safresh1} 416fb12b70Safresh1 42b8851fccSafresh1# For objects returned by pairs() 43b8851fccSafresh1sub List::Util::_Pair::key { shift->[0] } 44b8851fccSafresh1sub List::Util::_Pair::value { shift->[1] } 4556d68f1eSafresh1sub List::Util::_Pair::TO_JSON { [ @{+shift} ] } 46b8851fccSafresh1 476fb12b70Safresh1=head1 NAME 486fb12b70Safresh1 496fb12b70Safresh1List::Util - A selection of general-utility list subroutines 506fb12b70Safresh1 516fb12b70Safresh1=head1 SYNOPSIS 526fb12b70Safresh1 539f11ffb7Safresh1 use List::Util qw( 5456d68f1eSafresh1 reduce any all none notall first reductions 559f11ffb7Safresh1 569f11ffb7Safresh1 max maxstr min minstr product sum sum0 579f11ffb7Safresh1 589f11ffb7Safresh1 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap 599f11ffb7Safresh1 60eac174f2Safresh1 shuffle uniq uniqint uniqnum uniqstr zip mesh 619f11ffb7Safresh1 ); 626fb12b70Safresh1 636fb12b70Safresh1=head1 DESCRIPTION 646fb12b70Safresh1 656fb12b70Safresh1C<List::Util> contains a selection of subroutines that people have expressed 666fb12b70Safresh1would be nice to have in the perl core, but the usage would not really be high 676fb12b70Safresh1enough to warrant the use of a keyword, and the size so small such that being 686fb12b70Safresh1individual extensions would be wasteful. 696fb12b70Safresh1 706fb12b70Safresh1By default C<List::Util> does not export any subroutines. 716fb12b70Safresh1 726fb12b70Safresh1=cut 736fb12b70Safresh1 746fb12b70Safresh1=head1 LIST-REDUCTION FUNCTIONS 756fb12b70Safresh1 7656d68f1eSafresh1The following set of functions all apply a given block of code to a list of 7756d68f1eSafresh1values. 786fb12b70Safresh1 796fb12b70Safresh1=cut 806fb12b70Safresh1 819f11ffb7Safresh1=head2 reduce 829f11ffb7Safresh1 839f11ffb7Safresh1 $result = reduce { BLOCK } @list 846fb12b70Safresh1 856fb12b70Safresh1Reduces C<@list> by calling C<BLOCK> in a scalar context multiple times, 866fb12b70Safresh1setting C<$a> and C<$b> each time. The first call will be with C<$a> and C<$b> 876fb12b70Safresh1set to the first two elements of the list, subsequent calls will be done by 886fb12b70Safresh1setting C<$a> to the result of the previous call and C<$b> to the next element 896fb12b70Safresh1in the list. 906fb12b70Safresh1 916fb12b70Safresh1Returns the result of the last call to the C<BLOCK>. If C<@list> is empty then 926fb12b70Safresh1C<undef> is returned. If C<@list> only contains one element then that element 936fb12b70Safresh1is returned and C<BLOCK> is not executed. 946fb12b70Safresh1 956fb12b70Safresh1The following examples all demonstrate how C<reduce> could be used to implement 966fb12b70Safresh1the other list-reduction functions in this module. (They are not in fact 976fb12b70Safresh1implemented like this, but instead in a more efficient manner in individual C 986fb12b70Safresh1functions). 996fb12b70Safresh1 1006fb12b70Safresh1 $foo = reduce { defined($a) ? $a : 1016fb12b70Safresh1 $code->(local $_ = $b) ? $b : 1026fb12b70Safresh1 undef } undef, @list # first 1036fb12b70Safresh1 1046fb12b70Safresh1 $foo = reduce { $a > $b ? $a : $b } 1..10 # max 1056fb12b70Safresh1 $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z' # maxstr 1066fb12b70Safresh1 $foo = reduce { $a < $b ? $a : $b } 1..10 # min 1076fb12b70Safresh1 $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr 1086fb12b70Safresh1 $foo = reduce { $a + $b } 1 .. 10 # sum 1096fb12b70Safresh1 $foo = reduce { $a . $b } @bar # concat 1106fb12b70Safresh1 1116fb12b70Safresh1 $foo = reduce { $a || $code->(local $_ = $b) } 0, @bar # any 1126fb12b70Safresh1 $foo = reduce { $a && $code->(local $_ = $b) } 1, @bar # all 1136fb12b70Safresh1 $foo = reduce { $a && !$code->(local $_ = $b) } 1, @bar # none 1146fb12b70Safresh1 $foo = reduce { $a || !$code->(local $_ = $b) } 0, @bar # notall 1156fb12b70Safresh1 # Note that these implementations do not fully short-circuit 1166fb12b70Safresh1 1176fb12b70Safresh1If your algorithm requires that C<reduce> produce an identity value, then make 1186fb12b70Safresh1sure that you always pass that identity value as the first argument to prevent 1196fb12b70Safresh1C<undef> being returned 1206fb12b70Safresh1 1216fb12b70Safresh1 $foo = reduce { $a + $b } 0, @values; # sum with 0 identity value 1226fb12b70Safresh1 1239f11ffb7Safresh1The above example code blocks also suggest how to use C<reduce> to build a 1249f11ffb7Safresh1more efficient combined version of one of these basic functions and a C<map> 1259f11ffb7Safresh1block. For example, to find the total length of all the strings in a list, 1269f11ffb7Safresh1we could use 1279f11ffb7Safresh1 1289f11ffb7Safresh1 $total = sum map { length } @strings; 1299f11ffb7Safresh1 1309f11ffb7Safresh1However, this produces a list of temporary integer values as long as the 1319f11ffb7Safresh1original list of strings, only to reduce it down to a single value again. We 1329f11ffb7Safresh1can compute the same result more efficiently by using C<reduce> with a code 1339f11ffb7Safresh1block that accumulates lengths by writing this instead as: 1349f11ffb7Safresh1 1359f11ffb7Safresh1 $total = reduce { $a + length $b } 0, @strings 1369f11ffb7Safresh1 13756d68f1eSafresh1The other scalar-returning list reduction functions are all specialisations of 13856d68f1eSafresh1this generic idea. 13956d68f1eSafresh1 14056d68f1eSafresh1=head2 reductions 14156d68f1eSafresh1 14256d68f1eSafresh1 @results = reductions { BLOCK } @list 14356d68f1eSafresh1 14456d68f1eSafresh1I<Since version 1.54.> 14556d68f1eSafresh1 14656d68f1eSafresh1Similar to C<reduce> except that it also returns the intermediate values along 14756d68f1eSafresh1with the final result. As before, C<$a> is set to the first element of the 14856d68f1eSafresh1given list, and the C<BLOCK> is then called once for remaining item in the 14956d68f1eSafresh1list set into C<$b>, with the result being captured for return as well as 15056d68f1eSafresh1becoming the new value for C<$a>. 15156d68f1eSafresh1 15256d68f1eSafresh1The returned list will begin with the initial value for C<$a>, followed by 15356d68f1eSafresh1each return value from the block in order. The final value of the result will 15456d68f1eSafresh1be identical to what the C<reduce> function would have returned given the same 15556d68f1eSafresh1block and list. 15656d68f1eSafresh1 15756d68f1eSafresh1 reduce { "$a-$b" } "a".."d" # "a-b-c-d" 15856d68f1eSafresh1 reductions { "$a-$b" } "a".."d" # "a", "a-b", "a-b-c", "a-b-c-d" 1596fb12b70Safresh1 160b8851fccSafresh1=head2 any 161b8851fccSafresh1 162b8851fccSafresh1 my $bool = any { BLOCK } @list; 163b8851fccSafresh1 164b8851fccSafresh1I<Since version 1.33.> 1656fb12b70Safresh1 1666fb12b70Safresh1Similar to C<grep> in that it evaluates C<BLOCK> setting C<$_> to each element 1676fb12b70Safresh1of C<@list> in turn. C<any> returns true if any element makes the C<BLOCK> 1686fb12b70Safresh1return a true value. If C<BLOCK> never returns true or C<@list> was empty then 1696fb12b70Safresh1it returns false. 1706fb12b70Safresh1 1716fb12b70Safresh1Many cases of using C<grep> in a conditional can be written using C<any> 1726fb12b70Safresh1instead, as it can short-circuit after the first true result. 1736fb12b70Safresh1 1746fb12b70Safresh1 if( any { length > 10 } @strings ) { 1756fb12b70Safresh1 # at least one string has more than 10 characters 1766fb12b70Safresh1 } 1776fb12b70Safresh1 1789f11ffb7Safresh1Note: Due to XS issues the block passed may be able to access the outer @_ 1799f11ffb7Safresh1directly. This is not intentional and will break under debugger. 1809f11ffb7Safresh1 181b8851fccSafresh1=head2 all 1826fb12b70Safresh1 183b8851fccSafresh1 my $bool = all { BLOCK } @list; 1846fb12b70Safresh1 185b8851fccSafresh1I<Since version 1.33.> 1866fb12b70Safresh1 187b8851fccSafresh1Similar to L</any>, except that it requires all elements of the C<@list> to 188b8851fccSafresh1make the C<BLOCK> return true. If any element returns false, then it returns 189b8851fccSafresh1false. If the C<BLOCK> never returns false or the C<@list> was empty then it 190b8851fccSafresh1returns true. 1916fb12b70Safresh1 1929f11ffb7Safresh1Note: Due to XS issues the block passed may be able to access the outer @_ 1939f11ffb7Safresh1directly. This is not intentional and will break under debugger. 1949f11ffb7Safresh1 195b8851fccSafresh1=head2 none 1966fb12b70Safresh1 197b8851fccSafresh1=head2 notall 198b8851fccSafresh1 199b8851fccSafresh1 my $bool = none { BLOCK } @list; 200b8851fccSafresh1 201b8851fccSafresh1 my $bool = notall { BLOCK } @list; 202b8851fccSafresh1 203b8851fccSafresh1I<Since version 1.33.> 204b8851fccSafresh1 205b8851fccSafresh1Similar to L</any> and L</all>, but with the return sense inverted. C<none> 206b8851fccSafresh1returns true only if no value in the C<@list> causes the C<BLOCK> to return 207b8851fccSafresh1true, and C<notall> returns true only if not all of the values do. 208b8851fccSafresh1 2099f11ffb7Safresh1Note: Due to XS issues the block passed may be able to access the outer @_ 2109f11ffb7Safresh1directly. This is not intentional and will break under debugger. 2119f11ffb7Safresh1 212b8851fccSafresh1=head2 first 213b8851fccSafresh1 214b8851fccSafresh1 my $val = first { BLOCK } @list; 2156fb12b70Safresh1 2166fb12b70Safresh1Similar to C<grep> in that it evaluates C<BLOCK> setting C<$_> to each element 2176fb12b70Safresh1of C<@list> in turn. C<first> returns the first element where the result from 2186fb12b70Safresh1C<BLOCK> is a true value. If C<BLOCK> never returns true or C<@list> was empty 2196fb12b70Safresh1then C<undef> is returned. 2206fb12b70Safresh1 2216fb12b70Safresh1 $foo = first { defined($_) } @list # first defined value in @list 2226fb12b70Safresh1 $foo = first { $_ > $value } @list # first value in @list which 2236fb12b70Safresh1 # is greater than $value 2246fb12b70Safresh1 225b8851fccSafresh1=head2 max 226b8851fccSafresh1 227b8851fccSafresh1 my $num = max @list; 2286fb12b70Safresh1 2296fb12b70Safresh1Returns the entry in the list with the highest numerical value. If the list is 2306fb12b70Safresh1empty then C<undef> is returned. 2316fb12b70Safresh1 2326fb12b70Safresh1 $foo = max 1..10 # 10 2336fb12b70Safresh1 $foo = max 3,9,12 # 12 2346fb12b70Safresh1 $foo = max @bar, @baz # whatever 2356fb12b70Safresh1 236b8851fccSafresh1=head2 maxstr 2376fb12b70Safresh1 238b8851fccSafresh1 my $str = maxstr @list; 239b8851fccSafresh1 240b8851fccSafresh1Similar to L</max>, but treats all the entries in the list as strings and 2416fb12b70Safresh1returns the highest string as defined by the C<gt> operator. If the list is 2426fb12b70Safresh1empty then C<undef> is returned. 2436fb12b70Safresh1 2446fb12b70Safresh1 $foo = maxstr 'A'..'Z' # 'Z' 2456fb12b70Safresh1 $foo = maxstr "hello","world" # "world" 2466fb12b70Safresh1 $foo = maxstr @bar, @baz # whatever 2476fb12b70Safresh1 248b8851fccSafresh1=head2 min 2496fb12b70Safresh1 250b8851fccSafresh1 my $num = min @list; 251b8851fccSafresh1 252b8851fccSafresh1Similar to L</max> but returns the entry in the list with the lowest numerical 2536fb12b70Safresh1value. If the list is empty then C<undef> is returned. 2546fb12b70Safresh1 2556fb12b70Safresh1 $foo = min 1..10 # 1 2566fb12b70Safresh1 $foo = min 3,9,12 # 3 2576fb12b70Safresh1 $foo = min @bar, @baz # whatever 2586fb12b70Safresh1 259b8851fccSafresh1=head2 minstr 2606fb12b70Safresh1 261b8851fccSafresh1 my $str = minstr @list; 262b8851fccSafresh1 263b8851fccSafresh1Similar to L</min>, but treats all the entries in the list as strings and 2646fb12b70Safresh1returns the lowest string as defined by the C<lt> operator. If the list is 2656fb12b70Safresh1empty then C<undef> is returned. 2666fb12b70Safresh1 2676fb12b70Safresh1 $foo = minstr 'A'..'Z' # 'A' 2686fb12b70Safresh1 $foo = minstr "hello","world" # "hello" 2696fb12b70Safresh1 $foo = minstr @bar, @baz # whatever 2706fb12b70Safresh1 271b8851fccSafresh1=head2 product 272b8851fccSafresh1 273b8851fccSafresh1 my $num = product @list; 274b8851fccSafresh1 275b8851fccSafresh1I<Since version 1.35.> 2766fb12b70Safresh1 2776fb12b70Safresh1Returns the numerical product of all the elements in C<@list>. If C<@list> is 2786fb12b70Safresh1empty then C<1> is returned. 2796fb12b70Safresh1 2806fb12b70Safresh1 $foo = product 1..10 # 3628800 2816fb12b70Safresh1 $foo = product 3,9,12 # 324 2826fb12b70Safresh1 283b8851fccSafresh1=head2 sum 284b8851fccSafresh1 285b8851fccSafresh1 my $num_or_undef = sum @list; 2866fb12b70Safresh1 2876fb12b70Safresh1Returns the numerical sum of all the elements in C<@list>. For backwards 2886fb12b70Safresh1compatibility, if C<@list> is empty then C<undef> is returned. 2896fb12b70Safresh1 2906fb12b70Safresh1 $foo = sum 1..10 # 55 2916fb12b70Safresh1 $foo = sum 3,9,12 # 24 2926fb12b70Safresh1 $foo = sum @bar, @baz # whatever 2936fb12b70Safresh1 294b8851fccSafresh1=head2 sum0 2956fb12b70Safresh1 296b8851fccSafresh1 my $num = sum0 @list; 297b8851fccSafresh1 298b8851fccSafresh1I<Since version 1.26.> 299b8851fccSafresh1 300b8851fccSafresh1Similar to L</sum>, except this returns 0 when given an empty list, rather 301b8851fccSafresh1than C<undef>. 3026fb12b70Safresh1 3036fb12b70Safresh1=cut 3046fb12b70Safresh1 3056fb12b70Safresh1=head1 KEY/VALUE PAIR LIST FUNCTIONS 3066fb12b70Safresh1 3076fb12b70Safresh1The following set of functions, all inspired by L<List::Pairwise>, consume an 3086fb12b70Safresh1even-sized list of pairs. The pairs may be key/value associations from a hash, 3096fb12b70Safresh1or just a list of values. The functions will all preserve the original ordering 3106fb12b70Safresh1of the pairs, and will not be confused by multiple pairs having the same "key" 3116fb12b70Safresh1value - nor even do they require that the first of each pair be a plain string. 3126fb12b70Safresh1 313b8851fccSafresh1B<NOTE>: At the time of writing, the following C<pair*> functions that take a 314b8851fccSafresh1block do not modify the value of C<$_> within the block, and instead operate 315b8851fccSafresh1using the C<$a> and C<$b> globals instead. This has turned out to be a poor 316b8851fccSafresh1design, as it precludes the ability to provide a C<pairsort> function. Better 317b8851fccSafresh1would be to pass pair-like objects as 2-element array references in C<$_>, in 318b8851fccSafresh1a style similar to the return value of the C<pairs> function. At some future 319b8851fccSafresh1version this behaviour may be added. 320b8851fccSafresh1 321b8851fccSafresh1Until then, users are alerted B<NOT> to rely on the value of C<$_> remaining 322b8851fccSafresh1unmodified between the outside and the inside of the control block. In 323b8851fccSafresh1particular, the following example is B<UNSAFE>: 324b8851fccSafresh1 325b8851fccSafresh1 my @kvlist = ... 326b8851fccSafresh1 327b8851fccSafresh1 foreach (qw( some keys here )) { 328b8851fccSafresh1 my @items = pairgrep { $a eq $_ } @kvlist; 329b8851fccSafresh1 ... 330b8851fccSafresh1 } 331b8851fccSafresh1 332b8851fccSafresh1Instead, write this using a lexical variable: 333b8851fccSafresh1 334b8851fccSafresh1 foreach my $key (qw( some keys here )) { 335b8851fccSafresh1 my @items = pairgrep { $a eq $key } @kvlist; 336b8851fccSafresh1 ... 337b8851fccSafresh1 } 338b8851fccSafresh1 3396fb12b70Safresh1=cut 3406fb12b70Safresh1 341b8851fccSafresh1=head2 pairs 3426fb12b70Safresh1 343b8851fccSafresh1 my @pairs = pairs @kvlist; 344b8851fccSafresh1 345b8851fccSafresh1I<Since version 1.29.> 346b8851fccSafresh1 347b8851fccSafresh1A convenient shortcut to operating on even-sized lists of pairs, this function 3489f11ffb7Safresh1returns a list of C<ARRAY> references, each containing two items from the 3499f11ffb7Safresh1given list. It is a more efficient version of 350b8851fccSafresh1 351b8851fccSafresh1 @pairs = pairmap { [ $a, $b ] } @kvlist 352b8851fccSafresh1 353b8851fccSafresh1It is most convenient to use in a C<foreach> loop, for example: 354b8851fccSafresh1 3559f11ffb7Safresh1 foreach my $pair ( pairs @kvlist ) { 356b8851fccSafresh1 my ( $key, $value ) = @$pair; 357b8851fccSafresh1 ... 358b8851fccSafresh1 } 359b8851fccSafresh1 3609f11ffb7Safresh1Since version C<1.39> these C<ARRAY> references are blessed objects, 3619f11ffb7Safresh1recognising the two methods C<key> and C<value>. The following code is 3629f11ffb7Safresh1equivalent: 363b8851fccSafresh1 3649f11ffb7Safresh1 foreach my $pair ( pairs @kvlist ) { 365b8851fccSafresh1 my $key = $pair->key; 366b8851fccSafresh1 my $value = $pair->value; 367b8851fccSafresh1 ... 368b8851fccSafresh1 } 369b8851fccSafresh1 37056d68f1eSafresh1Since version C<1.51> they also have a C<TO_JSON> method to ease 37156d68f1eSafresh1serialisation. 37256d68f1eSafresh1 373b8851fccSafresh1=head2 unpairs 374b8851fccSafresh1 375b8851fccSafresh1 my @kvlist = unpairs @pairs 376b8851fccSafresh1 377b8851fccSafresh1I<Since version 1.42.> 378b8851fccSafresh1 3799f11ffb7Safresh1The inverse function to C<pairs>; this function takes a list of C<ARRAY> 380b8851fccSafresh1references containing two elements each, and returns a flattened list of the 381b8851fccSafresh1two values from each of the pairs, in order. This is notionally equivalent to 382b8851fccSafresh1 383b8851fccSafresh1 my @kvlist = map { @{$_}[0,1] } @pairs 384b8851fccSafresh1 385b8851fccSafresh1except that it is implemented more efficiently internally. Specifically, for 386b8851fccSafresh1any input item it will extract exactly two values for the output list; using 387b8851fccSafresh1C<undef> if the input array references are short. 388b8851fccSafresh1 389b8851fccSafresh1Between C<pairs> and C<unpairs>, a higher-order list function can be used to 390b8851fccSafresh1operate on the pairs as single scalars; such as the following near-equivalents 391b8851fccSafresh1of the other C<pair*> higher-order functions: 392b8851fccSafresh1 393b8851fccSafresh1 @kvlist = unpairs grep { FUNC } pairs @kvlist 394b8851fccSafresh1 # Like pairgrep, but takes $_ instead of $a and $b 395b8851fccSafresh1 396b8851fccSafresh1 @kvlist = unpairs map { FUNC } pairs @kvlist 397b8851fccSafresh1 # Like pairmap, but takes $_ instead of $a and $b 398b8851fccSafresh1 399b8851fccSafresh1Note however that these versions will not behave as nicely in scalar context. 400b8851fccSafresh1 401b8851fccSafresh1Finally, this technique can be used to implement a sort on a keyvalue pair 402b8851fccSafresh1list; e.g.: 403b8851fccSafresh1 404b8851fccSafresh1 @kvlist = unpairs sort { $a->key cmp $b->key } pairs @kvlist 405b8851fccSafresh1 406b8851fccSafresh1=head2 pairkeys 407b8851fccSafresh1 408b8851fccSafresh1 my @keys = pairkeys @kvlist; 409b8851fccSafresh1 410b8851fccSafresh1I<Since version 1.29.> 411b8851fccSafresh1 412b8851fccSafresh1A convenient shortcut to operating on even-sized lists of pairs, this function 413b8851fccSafresh1returns a list of the the first values of each of the pairs in the given list. 414b8851fccSafresh1It is a more efficient version of 415b8851fccSafresh1 416b8851fccSafresh1 @keys = pairmap { $a } @kvlist 417b8851fccSafresh1 418b8851fccSafresh1=head2 pairvalues 419b8851fccSafresh1 420b8851fccSafresh1 my @values = pairvalues @kvlist; 421b8851fccSafresh1 422b8851fccSafresh1I<Since version 1.29.> 423b8851fccSafresh1 424b8851fccSafresh1A convenient shortcut to operating on even-sized lists of pairs, this function 425b8851fccSafresh1returns a list of the the second values of each of the pairs in the given list. 426b8851fccSafresh1It is a more efficient version of 427b8851fccSafresh1 428b8851fccSafresh1 @values = pairmap { $b } @kvlist 429b8851fccSafresh1 430b8851fccSafresh1=head2 pairgrep 431b8851fccSafresh1 432b8851fccSafresh1 my @kvlist = pairgrep { BLOCK } @kvlist; 433b8851fccSafresh1 434b8851fccSafresh1 my $count = pairgrep { BLOCK } @kvlist; 435b8851fccSafresh1 436b8851fccSafresh1I<Since version 1.29.> 4376fb12b70Safresh1 4386fb12b70Safresh1Similar to perl's C<grep> keyword, but interprets the given list as an 4396fb12b70Safresh1even-sized list of pairs. It invokes the C<BLOCK> multiple times, in scalar 4406fb12b70Safresh1context, with C<$a> and C<$b> set to successive pairs of values from the 4416fb12b70Safresh1C<@kvlist>. 4426fb12b70Safresh1 4436fb12b70Safresh1Returns an even-sized list of those pairs for which the C<BLOCK> returned true 4446fb12b70Safresh1in list context, or the count of the B<number of pairs> in scalar context. 4456fb12b70Safresh1(Note, therefore, in scalar context that it returns a number half the size of 4466fb12b70Safresh1the count of items it would have returned in list context). 4476fb12b70Safresh1 4486fb12b70Safresh1 @subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist 4496fb12b70Safresh1 4506fb12b70Safresh1As with C<grep> aliasing C<$_> to list elements, C<pairgrep> aliases C<$a> and 4516fb12b70Safresh1C<$b> to elements of the given list. Any modifications of it by the code block 4526fb12b70Safresh1will be visible to the caller. 4536fb12b70Safresh1 454b8851fccSafresh1=head2 pairfirst 4556fb12b70Safresh1 456b8851fccSafresh1 my ( $key, $val ) = pairfirst { BLOCK } @kvlist; 4576fb12b70Safresh1 458b8851fccSafresh1 my $found = pairfirst { BLOCK } @kvlist; 459b8851fccSafresh1 460b8851fccSafresh1I<Since version 1.30.> 461b8851fccSafresh1 462b8851fccSafresh1Similar to the L</first> function, but interprets the given list as an 4636fb12b70Safresh1even-sized list of pairs. It invokes the C<BLOCK> multiple times, in scalar 4646fb12b70Safresh1context, with C<$a> and C<$b> set to successive pairs of values from the 4656fb12b70Safresh1C<@kvlist>. 4666fb12b70Safresh1 4676fb12b70Safresh1Returns the first pair of values from the list for which the C<BLOCK> returned 4686fb12b70Safresh1true in list context, or an empty list of no such pair was found. In scalar 4696fb12b70Safresh1context it returns a simple boolean value, rather than either the key or the 4706fb12b70Safresh1value found. 4716fb12b70Safresh1 4726fb12b70Safresh1 ( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist 4736fb12b70Safresh1 4746fb12b70Safresh1As with C<grep> aliasing C<$_> to list elements, C<pairfirst> aliases C<$a> and 4756fb12b70Safresh1C<$b> to elements of the given list. Any modifications of it by the code block 4766fb12b70Safresh1will be visible to the caller. 4776fb12b70Safresh1 478b8851fccSafresh1=head2 pairmap 4796fb12b70Safresh1 480b8851fccSafresh1 my @list = pairmap { BLOCK } @kvlist; 481b8851fccSafresh1 482b8851fccSafresh1 my $count = pairmap { BLOCK } @kvlist; 483b8851fccSafresh1 484b8851fccSafresh1I<Since version 1.29.> 4856fb12b70Safresh1 4866fb12b70Safresh1Similar to perl's C<map> keyword, but interprets the given list as an 4876fb12b70Safresh1even-sized list of pairs. It invokes the C<BLOCK> multiple times, in list 4886fb12b70Safresh1context, with C<$a> and C<$b> set to successive pairs of values from the 4896fb12b70Safresh1C<@kvlist>. 4906fb12b70Safresh1 4916fb12b70Safresh1Returns the concatenation of all the values returned by the C<BLOCK> in list 4926fb12b70Safresh1context, or the count of the number of items that would have been returned in 4936fb12b70Safresh1scalar context. 4946fb12b70Safresh1 4956fb12b70Safresh1 @result = pairmap { "The key $a has value $b" } @kvlist 4966fb12b70Safresh1 4976fb12b70Safresh1As with C<map> aliasing C<$_> to list elements, C<pairmap> aliases C<$a> and 4986fb12b70Safresh1C<$b> to elements of the given list. Any modifications of it by the code block 4996fb12b70Safresh1will be visible to the caller. 5006fb12b70Safresh1 501b8851fccSafresh1See L</KNOWN BUGS> for a known-bug with C<pairmap>, and a workaround. 5026fb12b70Safresh1 5036fb12b70Safresh1=cut 5046fb12b70Safresh1 5056fb12b70Safresh1=head1 OTHER FUNCTIONS 5066fb12b70Safresh1 5076fb12b70Safresh1=cut 5086fb12b70Safresh1 509b8851fccSafresh1=head2 shuffle 510b8851fccSafresh1 511b8851fccSafresh1 my @values = shuffle @values; 5126fb12b70Safresh1 5136fb12b70Safresh1Returns the values of the input in a random order 5146fb12b70Safresh1 5156fb12b70Safresh1 @cards = shuffle 0..51 # 0..51 in a random order 5166fb12b70Safresh1 51756d68f1eSafresh1This function is affected by the C<$RAND> variable. 51856d68f1eSafresh1 51956d68f1eSafresh1=cut 52056d68f1eSafresh1 52156d68f1eSafresh1=head2 sample 52256d68f1eSafresh1 52356d68f1eSafresh1 my @items = sample $count, @values 52456d68f1eSafresh1 52556d68f1eSafresh1I<Since version 1.54.> 52656d68f1eSafresh1 52756d68f1eSafresh1Randomly select the given number of elements from the input list. Any given 52856d68f1eSafresh1position in the input list will be selected at most once. 52956d68f1eSafresh1 53056d68f1eSafresh1If there are fewer than C<$count> items in the list then the function will 53156d68f1eSafresh1return once all of them have been randomly selected; effectively the function 53256d68f1eSafresh1behaves similarly to L</shuffle>. 53356d68f1eSafresh1 53456d68f1eSafresh1This function is affected by the C<$RAND> variable. 53556d68f1eSafresh1 5369f11ffb7Safresh1=head2 uniq 5379f11ffb7Safresh1 5389f11ffb7Safresh1 my @subset = uniq @values 5399f11ffb7Safresh1 5409f11ffb7Safresh1I<Since version 1.45.> 5419f11ffb7Safresh1 5429f11ffb7Safresh1Filters a list of values to remove subsequent duplicates, as judged by a 5439f11ffb7Safresh1DWIM-ish string equality or C<undef> test. Preserves the order of unique 5449f11ffb7Safresh1elements, and retains the first value of any duplicate set. 5459f11ffb7Safresh1 5469f11ffb7Safresh1 my $count = uniq @values 5479f11ffb7Safresh1 5489f11ffb7Safresh1In scalar context, returns the number of elements that would have been 5499f11ffb7Safresh1returned as a list. 5509f11ffb7Safresh1 5519f11ffb7Safresh1The C<undef> value is treated by this function as distinct from the empty 5529f11ffb7Safresh1string, and no warning will be produced. It is left as-is in the returned 5539f11ffb7Safresh1list. Subsequent C<undef> values are still considered identical to the first, 5549f11ffb7Safresh1and will be removed. 5559f11ffb7Safresh1 55656d68f1eSafresh1=head2 uniqint 55756d68f1eSafresh1 55856d68f1eSafresh1 my @subset = uniqint @values 55956d68f1eSafresh1 56056d68f1eSafresh1I<Since version 1.55.> 56156d68f1eSafresh1 56256d68f1eSafresh1Filters a list of values to remove subsequent duplicates, as judged by an 56356d68f1eSafresh1integer numerical equality test. Preserves the order of unique elements, and 56456d68f1eSafresh1retains the first value of any duplicate set. Values in the returned list will 56556d68f1eSafresh1be coerced into integers. 56656d68f1eSafresh1 56756d68f1eSafresh1 my $count = uniqint @values 56856d68f1eSafresh1 56956d68f1eSafresh1In scalar context, returns the number of elements that would have been 57056d68f1eSafresh1returned as a list. 57156d68f1eSafresh1 57256d68f1eSafresh1Note that C<undef> is treated much as other numerical operations treat it; it 57356d68f1eSafresh1compares equal to zero but additionally produces a warning if such warnings 57456d68f1eSafresh1are enabled (C<use warnings 'uninitialized';>). In addition, an C<undef> in 57556d68f1eSafresh1the returned list is coerced into a numerical zero, so that the entire list of 57656d68f1eSafresh1values returned by C<uniqint> are well-behaved as integers. 57756d68f1eSafresh1 5789f11ffb7Safresh1=head2 uniqnum 5799f11ffb7Safresh1 5809f11ffb7Safresh1 my @subset = uniqnum @values 5819f11ffb7Safresh1 5829f11ffb7Safresh1I<Since version 1.44.> 5839f11ffb7Safresh1 5849f11ffb7Safresh1Filters a list of values to remove subsequent duplicates, as judged by a 5859f11ffb7Safresh1numerical equality test. Preserves the order of unique elements, and retains 5869f11ffb7Safresh1the first value of any duplicate set. 5879f11ffb7Safresh1 5889f11ffb7Safresh1 my $count = uniqnum @values 5899f11ffb7Safresh1 5909f11ffb7Safresh1In scalar context, returns the number of elements that would have been 5919f11ffb7Safresh1returned as a list. 5929f11ffb7Safresh1 5939f11ffb7Safresh1Note that C<undef> is treated much as other numerical operations treat it; it 5949f11ffb7Safresh1compares equal to zero but additionally produces a warning if such warnings 5959f11ffb7Safresh1are enabled (C<use warnings 'uninitialized';>). In addition, an C<undef> in 5969f11ffb7Safresh1the returned list is coerced into a numerical zero, so that the entire list of 5979f11ffb7Safresh1values returned by C<uniqnum> are well-behaved as numbers. 5989f11ffb7Safresh1 5999f11ffb7Safresh1Note also that multiple IEEE C<NaN> values are treated as duplicates of 6009f11ffb7Safresh1each other, regardless of any differences in their payloads, and despite 6019f11ffb7Safresh1the fact that C<< 0+'NaN' == 0+'NaN' >> yields false. 6029f11ffb7Safresh1 6039f11ffb7Safresh1=head2 uniqstr 6049f11ffb7Safresh1 6059f11ffb7Safresh1 my @subset = uniqstr @values 6069f11ffb7Safresh1 6079f11ffb7Safresh1I<Since version 1.45.> 6089f11ffb7Safresh1 6099f11ffb7Safresh1Filters a list of values to remove subsequent duplicates, as judged by a 6109f11ffb7Safresh1string equality test. Preserves the order of unique elements, and retains the 6119f11ffb7Safresh1first value of any duplicate set. 6129f11ffb7Safresh1 6139f11ffb7Safresh1 my $count = uniqstr @values 6149f11ffb7Safresh1 6159f11ffb7Safresh1In scalar context, returns the number of elements that would have been 6169f11ffb7Safresh1returned as a list. 6179f11ffb7Safresh1 6189f11ffb7Safresh1Note that C<undef> is treated much as other string operations treat it; it 6199f11ffb7Safresh1compares equal to the empty string but additionally produces a warning if such 6209f11ffb7Safresh1warnings are enabled (C<use warnings 'uninitialized';>). In addition, an 6219f11ffb7Safresh1C<undef> in the returned list is coerced into an empty string, so that the 6229f11ffb7Safresh1entire list of values returned by C<uniqstr> are well-behaved as strings. 6239f11ffb7Safresh1 6246fb12b70Safresh1=cut 6256fb12b70Safresh1 6269f11ffb7Safresh1=head2 head 6279f11ffb7Safresh1 6289f11ffb7Safresh1 my @values = head $size, @list; 6299f11ffb7Safresh1 63056d68f1eSafresh1I<Since version 1.50.> 63156d68f1eSafresh1 6329f11ffb7Safresh1Returns the first C<$size> elements from C<@list>. If C<$size> is negative, returns 6339f11ffb7Safresh1all but the last C<$size> elements from C<@list>. 6349f11ffb7Safresh1 6359f11ffb7Safresh1 @result = head 2, qw( foo bar baz ); 6369f11ffb7Safresh1 # foo, bar 6379f11ffb7Safresh1 6389f11ffb7Safresh1 @result = head -2, qw( foo bar baz ); 6399f11ffb7Safresh1 # foo 6409f11ffb7Safresh1 6419f11ffb7Safresh1=head2 tail 6429f11ffb7Safresh1 6439f11ffb7Safresh1 my @values = tail $size, @list; 6449f11ffb7Safresh1 64556d68f1eSafresh1I<Since version 1.50.> 64656d68f1eSafresh1 6479f11ffb7Safresh1Returns the last C<$size> elements from C<@list>. If C<$size> is negative, returns 6489f11ffb7Safresh1all but the first C<$size> elements from C<@list>. 6499f11ffb7Safresh1 6509f11ffb7Safresh1 @result = tail 2, qw( foo bar baz ); 6519f11ffb7Safresh1 # bar, baz 6529f11ffb7Safresh1 6539f11ffb7Safresh1 @result = tail -2, qw( foo bar baz ); 6549f11ffb7Safresh1 # baz 6559f11ffb7Safresh1 656eac174f2Safresh1=head2 zip 657eac174f2Safresh1 658eac174f2Safresh1 my @result = zip [1..3], ['a'..'c']; 659eac174f2Safresh1 # [1, 'a'], [2, 'b'], [3, 'c'] 660eac174f2Safresh1 661eac174f2Safresh1I<Since version 1.56.> 662eac174f2Safresh1 663eac174f2Safresh1Returns a list of array references, composed of elements from the given list 664eac174f2Safresh1of array references. Each array in the returned list is composed of elements 665eac174f2Safresh1at that corresponding position from each of the given input arrays. If any 666eac174f2Safresh1input arrays run out of elements before others, then C<undef> will be inserted 667eac174f2Safresh1into the result to fill in the gaps. 668eac174f2Safresh1 669eac174f2Safresh1The C<zip> function is particularly handy for iterating over multiple arrays 670eac174f2Safresh1at the same time with a C<foreach> loop, taking one element from each: 671eac174f2Safresh1 672eac174f2Safresh1 foreach ( zip \@xs, \@ys, \@zs ) { 673eac174f2Safresh1 my ($x, $y, $z) = @$_; 674eac174f2Safresh1 ... 675eac174f2Safresh1 } 676eac174f2Safresh1 677eac174f2Safresh1B<NOTE> to users of L<List::MoreUtils>: This function does not behave the same 678eac174f2Safresh1as C<List::MoreUtils::zip>, but is actually a non-prototyped equivalent to 679eac174f2Safresh1C<List::MoreUtils::zip_unflatten>. This function does not apply a prototype, 680eac174f2Safresh1so make sure to invoke it with references to arrays. 681eac174f2Safresh1 682eac174f2Safresh1For a function similar to the C<zip> function from C<List::MoreUtils>, see 683eac174f2Safresh1L<mesh>. 684eac174f2Safresh1 685eac174f2Safresh1 my @result = zip_shortest ... 686eac174f2Safresh1 687eac174f2Safresh1A variation of the function that differs in how it behaves when given input 688eac174f2Safresh1arrays of differing lengths. C<zip_shortest> will stop as soon as any one of 689eac174f2Safresh1the input arrays run out of elements, discarding any remaining unused values 690eac174f2Safresh1from the others. 691eac174f2Safresh1 692eac174f2Safresh1 my @result = zip_longest ... 693eac174f2Safresh1 694eac174f2Safresh1C<zip_longest> is an alias to the C<zip> function, provided simply to be 695eac174f2Safresh1explicit about that behaviour as compared to C<zip_shortest>. 696eac174f2Safresh1 697eac174f2Safresh1=head2 mesh 698eac174f2Safresh1 699eac174f2Safresh1 my @result = mesh [1..3], ['a'..'c']; 700eac174f2Safresh1 # (1, 'a', 2, 'b', 3, 'c') 701eac174f2Safresh1 702eac174f2Safresh1I<Since version 1.56.> 703eac174f2Safresh1 704eac174f2Safresh1Returns a list of items collected from elements of the given list of array 705eac174f2Safresh1references. Each section of items in the returned list is composed of elements 706eac174f2Safresh1at the corresponding position from each of the given input arrays. If any 707eac174f2Safresh1input arrays run out of elements before others, then C<undef> will be inserted 708eac174f2Safresh1into the result to fill in the gaps. 709eac174f2Safresh1 710eac174f2Safresh1This is similar to L<zip>, except that all of the ranges in the result are 711eac174f2Safresh1returned in one long flattened list, instead of being bundled into separate 712eac174f2Safresh1arrays. 713eac174f2Safresh1 714eac174f2Safresh1Because it returns a flat list of items, the C<mesh> function is particularly 715eac174f2Safresh1useful for building a hash out of two separate arrays of keys and values: 716eac174f2Safresh1 717eac174f2Safresh1 my %hash = mesh \@keys, \@values; 718eac174f2Safresh1 719eac174f2Safresh1 my $href = { mesh \@keys, \@values }; 720eac174f2Safresh1 721eac174f2Safresh1B<NOTE> to users of L<List::MoreUtils>: This function is a non-prototyped 722eac174f2Safresh1equivalent to C<List::MoreUtils::mesh> or C<List::MoreUtils::zip> (themselves 723eac174f2Safresh1aliases of each other). This function does not apply a prototype, so make sure 724eac174f2Safresh1to invoke it with references to arrays. 725eac174f2Safresh1 726eac174f2Safresh1 my @result = mesh_shortest ... 727eac174f2Safresh1 728eac174f2Safresh1 my @result = mesh_longest ... 729eac174f2Safresh1 730eac174f2Safresh1These variations are similar to those of L<zip>, in that they differ in 731eac174f2Safresh1behaviour when one of the input lists runs out of elements before the others. 732eac174f2Safresh1 73356d68f1eSafresh1=head1 CONFIGURATION VARIABLES 73456d68f1eSafresh1 73556d68f1eSafresh1=head2 $RAND 73656d68f1eSafresh1 73756d68f1eSafresh1 local $List::Util::RAND = sub { ... }; 73856d68f1eSafresh1 73956d68f1eSafresh1I<Since version 1.54.> 74056d68f1eSafresh1 74156d68f1eSafresh1This package variable is used by code which needs to generate random numbers 74256d68f1eSafresh1(such as the L</shuffle> and L</sample> functions). If set to a CODE reference 74356d68f1eSafresh1it provides an alternative to perl's builtin C<rand()> function. When a new 74456d68f1eSafresh1random number is needed this function will be invoked with no arguments and is 74556d68f1eSafresh1expected to return a floating-point value, of which only the fractional part 74656d68f1eSafresh1will be used. 74756d68f1eSafresh1 7486fb12b70Safresh1=head1 KNOWN BUGS 7496fb12b70Safresh1 750b8851fccSafresh1=head2 RT #95409 751b8851fccSafresh1 752b8851fccSafresh1L<https://rt.cpan.org/Ticket/Display.html?id=95409> 753b8851fccSafresh1 754b8851fccSafresh1If the block of code given to L</pairmap> contains lexical variables that are 755b8851fccSafresh1captured by a returned closure, and the closure is executed after the block 756b8851fccSafresh1has been re-used for the next iteration, these lexicals will not see the 757b8851fccSafresh1correct values. For example: 758b8851fccSafresh1 759b8851fccSafresh1 my @subs = pairmap { 760b8851fccSafresh1 my $var = "$a is $b"; 761b8851fccSafresh1 sub { print "$var\n" }; 762b8851fccSafresh1 } one => 1, two => 2, three => 3; 763b8851fccSafresh1 764b8851fccSafresh1 $_->() for @subs; 765b8851fccSafresh1 766b8851fccSafresh1Will incorrectly print 767b8851fccSafresh1 768b8851fccSafresh1 three is 3 769b8851fccSafresh1 three is 3 770b8851fccSafresh1 three is 3 771b8851fccSafresh1 772b8851fccSafresh1This is due to the performance optimisation of using C<MULTICALL> for the code 773b8851fccSafresh1block, which means that fresh SVs do not get allocated for each call to the 774b8851fccSafresh1block. Instead, the same SV is re-assigned for each iteration, and all the 775b8851fccSafresh1closures will share the value seen on the final iteration. 776b8851fccSafresh1 777b8851fccSafresh1To work around this bug, surround the code with a second set of braces. This 778b8851fccSafresh1creates an inner block that defeats the C<MULTICALL> logic, and does get fresh 779b8851fccSafresh1SVs allocated each time: 780b8851fccSafresh1 781b8851fccSafresh1 my @subs = pairmap { 782b8851fccSafresh1 { 783b8851fccSafresh1 my $var = "$a is $b"; 784b8851fccSafresh1 sub { print "$var\n"; } 785b8851fccSafresh1 } 786b8851fccSafresh1 } one => 1, two => 2, three => 3; 787b8851fccSafresh1 788b8851fccSafresh1This bug only affects closures that are generated by the block but used 789b8851fccSafresh1afterwards. Lexical variables that are only used during the lifetime of the 790b8851fccSafresh1block's execution will take their individual values for each invocation, as 791b8851fccSafresh1normal. 7926fb12b70Safresh1 7939f11ffb7Safresh1=head2 uniqnum() on oversized bignums 7949f11ffb7Safresh1 7959f11ffb7Safresh1Due to the way that C<uniqnum()> compares numbers, it cannot distinguish 7969f11ffb7Safresh1differences between bignums (especially bigints) that are too large to fit in 7979f11ffb7Safresh1the native platform types. For example, 7989f11ffb7Safresh1 7999f11ffb7Safresh1 my $x = Math::BigInt->new( "1" x 100 ); 8009f11ffb7Safresh1 my $y = $x + 1; 8019f11ffb7Safresh1 8029f11ffb7Safresh1 say for uniqnum( $x, $y ); 8039f11ffb7Safresh1 8049f11ffb7Safresh1Will print just the value of C<$x>, believing that C<$y> is a numerically- 8059f11ffb7Safresh1equivalent value. This bug does not affect C<uniqstr()>, which will correctly 8069f11ffb7Safresh1observe that the two values stringify to different strings. 8079f11ffb7Safresh1 8086fb12b70Safresh1=head1 SUGGESTED ADDITIONS 8096fb12b70Safresh1 8106fb12b70Safresh1The following are additions that have been requested, but I have been reluctant 8116fb12b70Safresh1to add due to them being very simple to implement in perl 8126fb12b70Safresh1 8136fb12b70Safresh1 # How many elements are true 8146fb12b70Safresh1 8156fb12b70Safresh1 sub true { scalar grep { $_ } @_ } 8166fb12b70Safresh1 8176fb12b70Safresh1 # How many elements are false 8186fb12b70Safresh1 8196fb12b70Safresh1 sub false { scalar grep { !$_ } @_ } 8206fb12b70Safresh1 8216fb12b70Safresh1=head1 SEE ALSO 8226fb12b70Safresh1 8236fb12b70Safresh1L<Scalar::Util>, L<List::MoreUtils> 8246fb12b70Safresh1 8256fb12b70Safresh1=head1 COPYRIGHT 8266fb12b70Safresh1 8276fb12b70Safresh1Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. 8286fb12b70Safresh1This program is free software; you can redistribute it and/or 8296fb12b70Safresh1modify it under the same terms as Perl itself. 8306fb12b70Safresh1 8316fb12b70Safresh1Recent additions and current maintenance by 8326fb12b70Safresh1Paul Evans, <leonerd@leonerd.org.uk>. 8336fb12b70Safresh1 8346fb12b70Safresh1=cut 8359f11ffb7Safresh1 8369f11ffb7Safresh11; 837