File: Util.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (836 lines) | stat: -rw-r--r-- 66,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>List::Util - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="../static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="../static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="../index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="../index-overview.html">Overview</a>
                <li><a href="../index-tutorials.html">Tutorials</a>
                <li><a href="../index-faq.html">FAQs</a>
                <li><a href="../index-history.html">History / Changes</a>
                <li><a href="../index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="../index-language.html">Language</a>
                <li><a href="../index-functions.html">Functions</a>
                <li><a href="../perlop.html">Operators</a>
                <li><a href="../perlvar.html">Special Variables</a>
                <li><a href="../index-pragmas.html">Pragmas</a>
                <li><a href="../index-utilities.html">Utilities</a>
                <li><a href="../index-internals.html">Internals</a>
                <li><a href="../index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="../index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="../index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="../index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="../index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="../index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="../preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>List::Util</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="../search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="../index.html">Home</a> &gt;
    
      
        <a href="../index-modules-L.html">Core modules (L)</a> &gt;
      
    
    List::Util
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>List::Util</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#LIST-REDUCTION-FUNCTIONS">LIST-REDUCTION FUNCTIONS</a><ul><li><a href="#reduce">reduce</a><li><a href="#any">any</a><li><a href="#all">all</a><li><a href="#none">none</a><li><a href="#notall">notall</a><li><a href="#first">first</a><li><a href="#max">max</a><li><a href="#maxstr">maxstr</a><li><a href="#min">min</a><li><a href="#minstr">minstr</a><li><a href="#product">product</a><li><a href="#sum">sum</a><li><a href="#sum0">sum0</a></ul><li><a href="#KEY%2fVALUE-PAIR-LIST-FUNCTIONS">KEY/VALUE PAIR LIST FUNCTIONS</a><ul><li><a href="#pairs">pairs</a><li><a href="#unpairs">unpairs</a><li><a href="#pairkeys">pairkeys</a><li><a href="#pairvalues">pairvalues</a><li><a href="#pairgrep">pairgrep</a><li><a href="#pairfirst">pairfirst</a><li><a href="#pairmap">pairmap</a></ul><li><a href="#OTHER-FUNCTIONS">OTHER FUNCTIONS</a><ul><li><a href="#shuffle">shuffle</a><li><a href="#uniq">uniq</a><li><a href="#uniqnum">uniqnum</a><li><a href="#uniqstr">uniqstr</a></ul><li><a href="#KNOWN-BUGS">KNOWN BUGS</a><ul><li><a href="#RT-%2395409">RT #95409</a><li><a href="#uniqnum()-on-oversized-bignums">uniqnum() on oversized bignums</a></ul><li><a href="#SUGGESTED-ADDITIONS">SUGGESTED ADDITIONS</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#COPYRIGHT">COPYRIGHT</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>List::Util - A selection of general-utility list subroutines</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">List::Util</span> <span class="q">qw(</span></li><li>      <span class="q">reduce any all none notall first</span></li><li></li><li>      <span class="q">max maxstr min minstr product sum sum0</span></li><li></li><li>      <span class="q">pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap</span></li><li></li><li>      <span class="q">shuffle uniq uniqnum uniqstr</span></li><li>    <span class="q">)</span><span class="sc">;</span></li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p><code class="inline"><span class="w">List::Util</span></code>
 contains a selection of subroutines that people have expressed
would be nice to have in the perl core, but the usage would not really be high
enough to warrant the use of a keyword, and the size so small such that being
individual extensions would be wasteful.</p>
<p>By default <code class="inline"><span class="w">List::Util</span></code>
 does not export any subroutines.</p>
<a name="LIST-REDUCTION-FUNCTIONS"></a><h1>LIST-REDUCTION FUNCTIONS</h1>
<p>The following set of functions all reduce a list down to a single value.</p>
<a name="reduce"></a><h2>reduce</h2>
<pre class="verbatim"><ol><li>    <span class="i">$result</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@list</span></li></ol></pre><p>Reduces <code class="inline"><span class="i">@list</span></code>
 by calling <code class="inline"><span class="w">BLOCK</span></code>
 in a scalar context multiple times,
setting <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 each time. The first call will be with <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>

set to the first two elements of the list, subsequent calls will be done by
setting <code class="inline"><span class="i">$a</span></code>
 to the result of the previous call and <code class="inline"><span class="i">$b</span></code>
 to the next element
in the list.</p>
<p>Returns the result of the last call to the <code class="inline"><span class="w">BLOCK</span></code>
. If <code class="inline"><span class="i">@list</span></code>
 is empty then
<code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is returned. If <code class="inline"><span class="i">@list</span></code>
 only contains one element then that element
is returned and <code class="inline"><span class="w">BLOCK</span></code>
 is not executed.</p>
<p>The following examples all demonstrate how <code class="inline"><span class="w">reduce</span></code>
 could be used to implement
the other list-reduction functions in this module. (They are not in fact
implemented like this, but instead in a more efficient manner in individual C
functions).</p>
<pre class="verbatim"><ol><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span>            ? <span class="i">$a</span> <span class="co">:</span></li><li>                    <span class="i">$code</span>-&gt;<span class="s">(</span><a class="l_k" href="../functions/local.html">local</a> <span class="i">$_</span> = <span class="i">$b</span><span class="s">)</span> ? <span class="i">$b</span> <span class="co">:</span></li><li>                                             <a class="l_k" href="../functions/undef.html">undef</a> <span class="s">}</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="i">@list</span> <span class="c"># first</span></li><li></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> &gt; <span class="i">$b</span> ? <span class="i">$a</span> <span class="co">:</span> <span class="i">$b</span> <span class="s">}</span> <span class="n">1</span>..<span class="n">10</span>       <span class="c"># max</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> <a class="l_k" href="../functions/gt.html">gt</a> <span class="i">$b</span> ? <span class="i">$a</span> <span class="co">:</span> <span class="i">$b</span> <span class="s">}</span> <span class="q">&#39;A&#39;</span>..<span class="q">&#39;Z&#39;</span>   <span class="c"># maxstr</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> &lt; <span class="i">$b</span> ? <span class="i">$a</span> <span class="co">:</span> <span class="i">$b</span> <span class="s">}</span> <span class="n">1</span>..<span class="n">10</span>       <span class="c"># min</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> <a class="l_k" href="../functions/lt.html">lt</a> <span class="i">$b</span> ? <span class="i">$a</span> <span class="co">:</span> <span class="i">$b</span> <span class="s">}</span> <span class="q">&#39;aa&#39;</span>..<span class="q">&#39;zz&#39;</span> <span class="c"># minstr</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> + <span class="i">$b</span> <span class="s">}</span> <span class="n">1</span> .. <span class="n">10</span>               <span class="c"># sum</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> . <span class="i">$b</span> <span class="s">}</span> <span class="i">@bar</span>                  <span class="c"># concat</span></li><li></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> || <span class="i">$code</span>-&gt;<span class="s">(</span><a class="l_k" href="../functions/local.html">local</a> <span class="i">$_</span> = <span class="i">$b</span><span class="s">)</span> <span class="s">}</span> <span class="n">0</span><span class="cm">,</span> <span class="i">@bar</span>   <span class="c"># any</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> &amp;&amp; <span class="i">$code</span>-&gt;<span class="s">(</span><a class="l_k" href="../functions/local.html">local</a> <span class="i">$_</span> = <span class="i">$b</span><span class="s">)</span> <span class="s">}</span> <span class="n">1</span><span class="cm">,</span> <span class="i">@bar</span>   <span class="c"># all</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> &amp;&amp; !<span class="i">$code</span>-&gt;<span class="s">(</span><a class="l_k" href="../functions/local.html">local</a> <span class="i">$_</span> = <span class="i">$b</span><span class="s">)</span> <span class="s">}</span> <span class="n">1</span><span class="cm">,</span> <span class="i">@bar</span>  <span class="c"># none</span></li><li>    <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> || !<span class="i">$code</span>-&gt;<span class="s">(</span><a class="l_k" href="../functions/local.html">local</a> <span class="i">$_</span> = <span class="i">$b</span><span class="s">)</span> <span class="s">}</span> <span class="n">0</span><span class="cm">,</span> <span class="i">@bar</span>  <span class="c"># notall</span></li><li>       <span class="c"># Note that these implementations do not fully short-circuit</span></li></ol></pre><p>If your algorithm requires that <code class="inline"><span class="w">reduce</span></code>
 produce an identity value, then make
sure that you always pass that identity value as the first argument to prevent
<code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> being returned</p>
<pre class="verbatim"><ol><li>  <span class="i">$foo</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> + <span class="i">$b</span> <span class="s">}</span> <span class="n">0</span><span class="cm">,</span> <span class="i">@values</span><span class="sc">;</span>             <span class="c"># sum with 0 identity value</span></li></ol></pre><p>The above example code blocks also suggest how to use <code class="inline"><span class="w">reduce</span></code>
 to build a
more efficient combined version of one of these basic functions and a <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code>
block. For example, to find the total length of the all the strings in a list,
we could use</p>
<pre class="verbatim"><ol><li>    <span class="i">$total</span> = <span class="w">sum</span> <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <a class="l_k" href="../functions/length.html">length</a> <span class="s">}</span> <span class="i">@strings</span><span class="sc">;</span></li></ol></pre><p>However, this produces a list of temporary integer values as long as the
original list of strings, only to reduce it down to a single value again. We
can compute the same result more efficiently by using <code class="inline"><span class="w">reduce</span></code>
 with a code
block that accumulates lengths by writing this instead as:</p>
<pre class="verbatim"><ol><li>    <span class="i">$total</span> = <span class="i">reduce</span> <span class="s">{</span> <span class="i">$a</span> + <a class="l_k" href="../functions/length.html">length</a> <span class="i">$b</span> <span class="s">}</span> <span class="n">0</span><span class="cm">,</span> <span class="i">@strings</span></li></ol></pre><p>The remaining list-reduction functions are all specialisations of this generic
idea.</p>
<a name="any"></a><h2>any</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$bool</span> = <span class="i">any</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.33.</i></p>
<p>Similar to <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> in that it evaluates <code class="inline"><span class="w">BLOCK</span></code>
 setting <code class="inline"><span class="i">$_</span></code>
 to each element
of <code class="inline"><span class="i">@list</span></code>
 in turn. <code class="inline"><span class="w">any</span></code>
 returns true if any element makes the <code class="inline"><span class="w">BLOCK</span></code>

return a true value. If <code class="inline"><span class="w">BLOCK</span></code>
 never returns true or <code class="inline"><span class="i">@list</span></code>
 was empty then
it returns false.</p>
<p>Many cases of using <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> in a conditional can be written using <code class="inline"><span class="w">any</span></code>

instead, as it can short-circuit after the first true result.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/if.html">if</a><span class="s">(</span> <span class="i">any</span> <span class="s">{</span> <a class="l_k" href="../functions/length.html">length</a> &gt; <span class="n">10</span> <span class="s">}</span> <span class="i">@strings</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># at least one string has more than 10 characters</span></li><li>    <span class="s">}</span></li></ol></pre><a name="all"></a><h2>all</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$bool</span> = <span class="i">all</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.33.</i></p>
<p>Similar to <a href="#any">any</a>, except that it requires all elements of the <code class="inline"><span class="i">@list</span></code>
 to
make the <code class="inline"><span class="w">BLOCK</span></code>
 return true. If any element returns false, then it returns
false. If the <code class="inline"><span class="w">BLOCK</span></code>
 never returns false or the <code class="inline"><span class="i">@list</span></code>
 was empty then it
returns true.</p>
<a name="none"></a><h2>none</h2>
<a name="notall"></a><h2>notall</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$bool</span> = <span class="i">none</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@list</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$bool</span> = <span class="i">notall</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.33.</i></p>
<p>Similar to <a href="#any">any</a> and <a href="#all">all</a>, but with the return sense inverted. <code class="inline"><span class="w">none</span></code>

returns true only if no value in the <code class="inline"><span class="i">@list</span></code>
 causes the <code class="inline"><span class="w">BLOCK</span></code>
 to return
true, and <code class="inline"><span class="w">notall</span></code>
 returns true only if not all of the values do.</p>
<a name="first"></a><h2>first</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$val</span> = <span class="i">first</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p>Similar to <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> in that it evaluates <code class="inline"><span class="w">BLOCK</span></code>
 setting <code class="inline"><span class="i">$_</span></code>
 to each element
of <code class="inline"><span class="i">@list</span></code>
 in turn. <code class="inline"><span class="w">first</span></code>
 returns the first element where the result from
<code class="inline"><span class="w">BLOCK</span></code>
 is a true value. If <code class="inline"><span class="w">BLOCK</span></code>
 never returns true or <code class="inline"><span class="i">@list</span></code>
 was empty
then <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is returned.</p>
<pre class="verbatim"><ol><li>    $foo = first { defined($_) } @list    # first defined value in @list</li><li>    $foo = first { $_ &gt; $value } @list    # first value in @list which</li><li>                                          # is greater than $value</li></ol></pre><a name="max"></a><h2>max</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$num</span> = <span class="w">max</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p>Returns the entry in the list with the highest numerical value. If the list is
empty then <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is returned.</p>
<pre class="verbatim"><ol><li>    $foo = max 1..10                # 10</li><li>    $foo = max 3,9,12               # 12</li><li>    $foo = max @bar, @baz           # whatever</li></ol></pre><a name="maxstr"></a><h2>maxstr</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$str</span> = <span class="w">maxstr</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p>Similar to <a href="#max">max</a>, but treats all the entries in the list as strings and
returns the highest string as defined by the <code class="inline">gt</code>
 operator. If the list is
empty then <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is returned.</p>
<pre class="verbatim"><ol><li>    $foo = maxstr 'A'..'Z'          # 'Z'</li><li>    $foo = maxstr "hello","world"   # "world"</li><li>    $foo = maxstr @bar, @baz        # whatever</li></ol></pre><a name="min"></a><h2>min</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$num</span> = <span class="w">min</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p>Similar to <a href="#max">max</a> but returns the entry in the list with the lowest numerical
value. If the list is empty then <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is returned.</p>
<pre class="verbatim"><ol><li>    $foo = min 1..10                # 1</li><li>    $foo = min 3,9,12               # 3</li><li>    $foo = min @bar, @baz           # whatever</li></ol></pre><a name="minstr"></a><h2>minstr</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$str</span> = <span class="w">minstr</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p>Similar to <a href="#min">min</a>, but treats all the entries in the list as strings and
returns the lowest string as defined by the <code class="inline">lt</code>
 operator. If the list is
empty then <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is returned.</p>
<pre class="verbatim"><ol><li>    $foo = minstr 'A'..'Z'          # 'A'</li><li>    $foo = minstr "hello","world"   # "hello"</li><li>    $foo = minstr @bar, @baz        # whatever</li></ol></pre><a name="product"></a><h2>product</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$num</span> = <span class="w">product</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.35.</i></p>
<p>Returns the numerical product of all the elements in <code class="inline"><span class="i">@list</span></code>
. If <code class="inline"><span class="i">@list</span></code>
 is
empty then <code class="inline"><span class="n">1</span></code>
 is returned.</p>
<pre class="verbatim"><ol><li>    <span class="i">$foo</span> = <span class="w">product</span> <span class="n">1</span>..<span class="n">10</span>            <span class="c"># 3628800</span></li><li>    <span class="i">$foo</span> = <span class="w">product</span> <span class="n">3</span><span class="cm">,</span><span class="n">9</span><span class="cm">,</span><span class="n">12</span>           <span class="c"># 324</span></li></ol></pre><a name="sum"></a><h2>sum</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$num_or_undef</span> = <span class="w">sum</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p>Returns the numerical sum of all the elements in <code class="inline"><span class="i">@list</span></code>
. For backwards
compatibility, if <code class="inline"><span class="i">@list</span></code>
 is empty then <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is returned.</p>
<pre class="verbatim"><ol><li>    $foo = sum 1..10                # 55</li><li>    $foo = sum 3,9,12               # 24</li><li>    $foo = sum @bar, @baz           # whatever</li></ol></pre><a name="sum0"></a><h2>sum0</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$num</span> = <span class="w">sum0</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.26.</i></p>
<p>Similar to <a href="#sum">sum</a>, except this returns 0 when given an empty list, rather
than <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.</p>
<a name="KEY%2fVALUE-PAIR-LIST-FUNCTIONS"></a><h1>KEY/VALUE PAIR LIST FUNCTIONS</h1>
<p>The following set of functions, all inspired by <a href="http://search.cpan.org/perldoc/List::Pairwise">List::Pairwise</a>, consume an
even-sized list of pairs. The pairs may be key/value associations from a hash,
or just a list of values. The functions will all preserve the original ordering
of the pairs, and will not be confused by multiple pairs having the same "key"
value - nor even do they require that the first of each pair be a plain string.</p>
<p><b>NOTE</b>: At the time of writing, the following <code class="inline"><span class="w">pair</span>*</code>
 functions that take a
block do not modify the value of <code class="inline"><span class="i">$_</span></code>
 within the block, and instead operate
using the <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 globals instead. This has turned out to be a poor
design, as it precludes the ability to provide a <code class="inline"><span class="w">pairsort</span></code>
 function. Better
would be to pass pair-like objects as 2-element array references in <code class="inline"><span class="i">$_</span></code>
, in
a style similar to the return value of the <code class="inline"><span class="w">pairs</span></code>
 function. At some future
version this behaviour may be added.</p>
<p>Until then, users are alerted <b>NOT</b> to rely on the value of <code class="inline"><span class="i">$_</span></code>
 remaining
unmodified between the outside and the inside of the control block. In
particular, the following example is <b>UNSAFE</b>:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/my.html">my</a> <span class="i">@kvlist</span> = ...</li><li></li><li> <a class="l_k" href="../functions/foreach.html">foreach</a> <span class="s">(</span><span class="q">qw( some keys here )</span><span class="s">)</span> <span class="s">{</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@items</span> = <span class="i">pairgrep</span> <span class="s">{</span> <span class="i">$a</span> <a class="l_k" href="../functions/eq.html">eq</a> <span class="i">$_</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li><li>    ...</li><li> <span class="s">}</span></li></ol></pre><p>Instead, write this using a lexical variable:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/foreach.html">foreach</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$key</span> <span class="s">(</span><span class="q">qw( some keys here )</span><span class="s">)</span> <span class="s">{</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@items</span> = <span class="i">pairgrep</span> <span class="s">{</span> <span class="i">$a</span> <a class="l_k" href="../functions/eq.html">eq</a> <span class="i">$key</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li><li>    ...</li><li> <span class="s">}</span></li></ol></pre><a name="pairs"></a><h2>pairs</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@pairs</span> = <span class="w">pairs</span> <span class="i">@kvlist</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.29.</i></p>
<p>A convenient shortcut to operating on even-sized lists of pairs, this function
returns a list of <code class="inline"><span class="w">ARRAY</span></code>
 references, each containing two items from the
given list. It is a more efficient version of</p>
<pre class="verbatim"><ol><li>    <span class="i">@pairs</span> = <span class="i">pairmap</span> <span class="s">{</span> <span class="s">[</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span> <span class="s">]</span> <span class="s">}</span> <span class="i">@kvlist</span></li></ol></pre><p>It is most convenient to use in a <code class="inline">foreach</code>
 loop, for example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/foreach.html">foreach</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$pair</span> <span class="s">(</span> <span class="w">pairs</span> <span class="i">@kvlist</span> <span class="s">)</span> <span class="s">{</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span> <span class="i">$key</span><span class="cm">,</span> <span class="i">$value</span> <span class="s">)</span> = <span class="i">@$pair</span><span class="sc">;</span></li><li>       ...</li><li>    <span class="s">}</span></li></ol></pre><p>Since version <code class="inline"><span class="n">1.39</span></code>
 these <code class="inline"><span class="w">ARRAY</span></code>
 references are blessed objects,
recognising the two methods <code class="inline"><span class="w">key</span></code>
 and <code class="inline"><span class="w">value</span></code>
. The following code is
equivalent:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/foreach.html">foreach</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$pair</span> <span class="s">(</span> <span class="w">pairs</span> <span class="i">@kvlist</span> <span class="s">)</span> <span class="s">{</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="i">$key</span>   = <span class="i">$pair</span><span class="i">-&gt;key</span><span class="sc">;</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="i">$value</span> = <span class="i">$pair</span><span class="i">-&gt;value</span><span class="sc">;</span></li><li>       ...</li><li>    <span class="s">}</span></li></ol></pre><a name="unpairs"></a><h2>unpairs</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@kvlist</span> = <span class="w">unpairs</span> <span class="i">@pairs</span></li></ol></pre><p><i>Since version 1.42.</i></p>
<p>The inverse function to <code class="inline"><span class="w">pairs</span></code>
; this function takes a list of <code class="inline"><span class="w">ARRAY</span></code>

references containing two elements each, and returns a flattened list of the
two values from each of the pairs, in order. This is notionally equivalent to</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@kvlist</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="i">@</span>{<span class="i">$_</span>}[<span class="n">0</span><span class="cm">,</span><span class="n">1</span>] <span class="s">}</span> <span class="i">@pairs</span></li></ol></pre><p>except that it is implemented more efficiently internally. Specifically, for
any input item it will extract exactly two values for the output list; using
<code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if the input array references are short.</p>
<p>Between <code class="inline"><span class="w">pairs</span></code>
 and <code class="inline"><span class="w">unpairs</span></code>
, a higher-order list function can be used to
operate on the pairs as single scalars; such as the following near-equivalents
of the other <code class="inline"><span class="w">pair</span>*</code>
 higher-order functions:</p>
<pre class="verbatim"><ol><li>    <span class="i">@kvlist</span> = <span class="w">unpairs</span> <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span> <span class="w">FUNC</span> <span class="s">}</span> <span class="w">pairs</span> <span class="i">@kvlist</span></li><li>    <span class="c"># Like pairgrep, but takes $_ instead of $a and $b</span></li><li></li><li>    <span class="i">@kvlist</span> = <span class="w">unpairs</span> <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="w">FUNC</span> <span class="s">}</span> <span class="w">pairs</span> <span class="i">@kvlist</span></li><li>    <span class="c"># Like pairmap, but takes $_ instead of $a and $b</span></li></ol></pre><p>Note however that these versions will not behave as nicely in scalar context.</p>
<p>Finally, this technique can be used to implement a sort on a keyvalue pair
list; e.g.:</p>
<pre class="verbatim"><ol><li>    <span class="i">@kvlist</span> = <span class="w">unpairs</span> <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span><span class="i">-&gt;key</span> <a class="l_k" href="../functions/cmp.html">cmp</a> <span class="i">$b</span><span class="i">-&gt;key</span> <span class="s">}</span> <span class="w">pairs</span> <span class="i">@kvlist</span></li></ol></pre><a name="pairkeys"></a><h2>pairkeys</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@keys</span> = <span class="w">pairkeys</span> <span class="i">@kvlist</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.29.</i></p>
<p>A convenient shortcut to operating on even-sized lists of pairs, this function
returns a list of the the first values of each of the pairs in the given list.
It is a more efficient version of</p>
<pre class="verbatim"><ol><li>    <span class="i">@keys</span> = <span class="i">pairmap</span> <span class="s">{</span> <span class="i">$a</span> <span class="s">}</span> <span class="i">@kvlist</span></li></ol></pre><a name="pairvalues"></a><h2>pairvalues</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@values</span> = <span class="w">pairvalues</span> <span class="i">@kvlist</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.29.</i></p>
<p>A convenient shortcut to operating on even-sized lists of pairs, this function
returns a list of the the second values of each of the pairs in the given list.
It is a more efficient version of</p>
<pre class="verbatim"><ol><li>    <span class="i">@values</span> = <span class="i">pairmap</span> <span class="s">{</span> <span class="i">$b</span> <span class="s">}</span> <span class="i">@kvlist</span></li></ol></pre><a name="pairgrep"></a><h2>pairgrep</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@kvlist</span> = <span class="i">pairgrep</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$count</span> = <span class="i">pairgrep</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.29.</i></p>
<p>Similar to perl's <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> keyword, but interprets the given list as an
even-sized list of pairs. It invokes the <code class="inline"><span class="w">BLOCK</span></code>
 multiple times, in scalar
context, with <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 set to successive pairs of values from the
<code class="inline"><span class="i">@kvlist</span></code>
.</p>
<p>Returns an even-sized list of those pairs for which the <code class="inline"><span class="w">BLOCK</span></code>
 returned true
in list context, or the count of the <b>number of pairs</b> in scalar context.
(Note, therefore, in scalar context that it returns a number half the size of
the count of items it would have returned in list context).</p>
<pre class="verbatim"><ol><li>    <span class="i">@subset</span> = <span class="i">pairgrep</span> <span class="s">{</span> <span class="i">$a</span> =~ <span class="q">m/^[[:upper:]]+$/</span> <span class="s">}</span> <span class="i">@kvlist</span></li></ol></pre><p>As with <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> aliasing <code class="inline"><span class="i">$_</span></code>
 to list elements, <code class="inline"><span class="w">pairgrep</span></code>
 aliases <code class="inline"><span class="i">$a</span></code>
 and
<code class="inline"><span class="i">$b</span></code>
 to elements of the given list. Any modifications of it by the code block
will be visible to the caller.</p>
<a name="pairfirst"></a><h2>pairfirst</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span> <span class="i">$key</span><span class="cm">,</span> <span class="i">$val</span> <span class="s">)</span> = <span class="i">pairfirst</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$found</span> = <span class="i">pairfirst</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.30.</i></p>
<p>Similar to the <a href="#first">first</a> function, but interprets the given list as an
even-sized list of pairs. It invokes the <code class="inline"><span class="w">BLOCK</span></code>
 multiple times, in scalar
context, with <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 set to successive pairs of values from the
<code class="inline"><span class="i">@kvlist</span></code>
.</p>
<p>Returns the first pair of values from the list for which the <code class="inline"><span class="w">BLOCK</span></code>
 returned
true in list context, or an empty list of no such pair was found. In scalar
context it returns a simple boolean value, rather than either the key or the
value found.</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span> <span class="i">$key</span><span class="cm">,</span> <span class="i">$value</span> <span class="s">)</span> = <span class="i">pairfirst</span> <span class="s">{</span> <span class="i">$a</span> =~ <span class="q">m/^[[:upper:]]+$/</span> <span class="s">}</span> <span class="i">@kvlist</span></li></ol></pre><p>As with <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> aliasing <code class="inline"><span class="i">$_</span></code>
 to list elements, <code class="inline"><span class="w">pairfirst</span></code>
 aliases <code class="inline"><span class="i">$a</span></code>
 and
<code class="inline"><span class="i">$b</span></code>
 to elements of the given list. Any modifications of it by the code block
will be visible to the caller.</p>
<a name="pairmap"></a><h2>pairmap</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@list</span> = <span class="i">pairmap</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$count</span> = <span class="i">pairmap</span> <span class="s">{</span> <span class="w">BLOCK</span> <span class="s">}</span> <span class="i">@kvlist</span><span class="sc">;</span></li></ol></pre><p><i>Since version 1.29.</i></p>
<p>Similar to perl's <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code> keyword, but interprets the given list as an
even-sized list of pairs. It invokes the <code class="inline"><span class="w">BLOCK</span></code>
 multiple times, in list
context, with <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 set to successive pairs of values from the
<code class="inline"><span class="i">@kvlist</span></code>
.</p>
<p>Returns the concatenation of all the values returned by the <code class="inline"><span class="w">BLOCK</span></code>
 in list
context, or the count of the number of items that would have been returned in
scalar context.</p>
<pre class="verbatim"><ol><li>    <span class="i">@result</span> = <span class="i">pairmap</span> <span class="s">{</span> <span class="q">&quot;The key $a has value $b&quot;</span> <span class="s">}</span> <span class="i">@kvlist</span></li></ol></pre><p>As with <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code> aliasing <code class="inline"><span class="i">$_</span></code>
 to list elements, <code class="inline"><span class="w">pairmap</span></code>
 aliases <code class="inline"><span class="i">$a</span></code>
 and
<code class="inline"><span class="i">$b</span></code>
 to elements of the given list. Any modifications of it by the code block
will be visible to the caller.</p>
<p>See <a href="#KNOWN-BUGS">KNOWN BUGS</a> for a known-bug with <code class="inline"><span class="w">pairmap</span></code>
, and a workaround.</p>
<a name="OTHER-FUNCTIONS"></a><h1>OTHER FUNCTIONS</h1>
<a name="shuffle"></a><h2>shuffle</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@values</span> = <span class="w">shuffle</span> <span class="i">@values</span><span class="sc">;</span></li></ol></pre><p>Returns the values of the input in a random order</p>
<pre class="verbatim"><ol><li>    <span class="i">@cards</span> = <span class="w">shuffle</span> <span class="n">0</span>..<span class="n">51</span>      <span class="c"># 0..51 in a random order</span></li></ol></pre><a name="uniq"></a><h2>uniq</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@subset</span> = <span class="w">uniq</span> <span class="i">@values</span></li></ol></pre><p><i>Since version 1.45.</i></p>
<p>Filters a list of values to remove subsequent duplicates, as judged by a
DWIM-ish string equality or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> test. Preserves the order of unique
elements, and retains the first value of any duplicate set.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$count</span> = <span class="w">uniq</span> <span class="i">@values</span></li></ol></pre><p>In scalar context, returns the number of elements that would have been
returned as a list.</p>
<p>The <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> value is treated by this function as distinct from the empty
string, and no warning will be produced. It is left as-is in the returned
list. Subsequent <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> values are still considered identical to the first,
and will be removed.</p>
<a name="uniqnum"></a><h2>uniqnum</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@subset</span> = <span class="w">uniqnum</span> <span class="i">@values</span></li></ol></pre><p><i>Since version 1.44.</i></p>
<p>Filters a list of values to remove subsequent duplicates, as judged by a
numerical equality test. Preserves the order of unique elements, and retains
the first value of any duplicate set.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$count</span> = <span class="w">uniqnum</span> <span class="i">@values</span></li></ol></pre><p>In scalar context, returns the number of elements that would have been
returned as a list.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is treated much as other numerical operations treat it; it
compares equal to zero but additionally produces a warning if such warnings
are enabled (<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;uninitialized&#39;</span><span class="sc">;</span></code>
). In addition, an <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> in
the returned list is coerced into a numerical zero, so that the entire list of
values returned by <code class="inline"><span class="w">uniqnum</span></code>
 are well-behaved as numbers.</p>
<p>Note also that multiple IEEE <code class="inline"><span class="w">NaN</span></code>
 values are treated as duplicates of
each other, regardless of any differences in their payloads, and despite
the fact that <code class="inline"><span class="n">0</span>+<span class="q">&#39;NaN&#39;</span> == <span class="n">0</span>+<span class="q">&#39;NaN&#39;</span></code>
 yields false.</p>
<a name="uniqstr"></a><h2>uniqstr</h2>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@subset</span> = <span class="w">uniqstr</span> <span class="i">@values</span></li></ol></pre><p><i>Since version 1.45.</i></p>
<p>Filters a list of values to remove subsequent duplicates, as judged by a
string equality test. Preserves the order of unique elements, and retains the
first value of any duplicate set.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$count</span> = <span class="w">uniqstr</span> <span class="i">@values</span></li></ol></pre><p>In scalar context, returns the number of elements that would have been
returned as a list.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is treated much as other string operations treat it; it
compares equal to the empty string but additionally produces a warning if such
warnings are enabled (<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;uninitialized&#39;</span><span class="sc">;</span></code>
). In addition, an
<code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> in the returned list is coerced into an empty string, so that the
entire list of values returned by <code class="inline"><span class="w">uniqstr</span></code>
 are well-behaved as strings.</p>
<a name="KNOWN-BUGS"></a><h1>KNOWN BUGS</h1>
<a name="RT-%2395409"></a><h2>RT #95409</h2>
<p><a href="https://rt.cpan.org/Ticket/Display.html?id=95409">https://rt.cpan.org/Ticket/Display.html?id=95409</a></p>
<p>If the block of code given to <a href="#pairmap">pairmap</a> contains lexical variables that are
captured by a returned closure, and the closure is executed after the block
has been re-used for the next iteration, these lexicals will not see the
correct values. For example:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/my.html">my</a> <span class="i">@subs</span> = <span class="i">pairmap</span> <span class="s">{</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$var</span> = <span class="q">&quot;$a is $b&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$var\n&quot;</span> <span class="s">}</span><span class="sc">;</span></li><li> <span class="s">}</span> <span class="w">one</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="cm">,</span> <span class="w">two</span> <span class="cm">=&gt;</span> <span class="n">2</span><span class="cm">,</span> <span class="w">three</span> <span class="cm">=&gt;</span> <span class="n">3</span><span class="sc">;</span></li><li></li><li> <span class="i">$_</span>-&gt;<span class="s">(</span><span class="s">)</span> <a class="l_k" href="../functions/for.html">for</a> <span class="i">@subs</span><span class="sc">;</span></li></ol></pre><p>Will incorrectly print</p>
<pre class="verbatim"><ol><li> <span class="w">three</span> <span class="w">is</span> <span class="n">3</span></li><li> <span class="w">three</span> <span class="w">is</span> <span class="n">3</span></li><li> <span class="w">three</span> <span class="w">is</span> <span class="n">3</span></li></ol></pre><p>This is due to the performance optimisation of using <code class="inline"><span class="w">MULTICALL</span></code>
 for the code
block, which means that fresh SVs do not get allocated for each call to the
block. Instead, the same SV is re-assigned for each iteration, and all the
closures will share the value seen on the final iteration.</p>
<p>To work around this bug, surround the code with a second set of braces. This
creates an inner block that defeats the <code class="inline"><span class="w">MULTICALL</span></code>
 logic, and does get fresh
SVs allocated each time:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/my.html">my</a> <span class="i">@subs</span> = <span class="i">pairmap</span> <span class="s">{</span></li><li>    <span class="s">{</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="i">$var</span> = <span class="q">&quot;$a is $b&quot;</span><span class="sc">;</span></li><li>       <a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$var\n&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <span class="s">}</span></li><li> <span class="s">}</span> <span class="w">one</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="cm">,</span> <span class="w">two</span> <span class="cm">=&gt;</span> <span class="n">2</span><span class="cm">,</span> <span class="w">three</span> <span class="cm">=&gt;</span> <span class="n">3</span><span class="sc">;</span></li></ol></pre><p>This bug only affects closures that are generated by the block but used
afterwards. Lexical variables that are only used during the lifetime of the
block's execution will take their individual values for each invocation, as
normal.</p>
<a name="uniqnum()-on-oversized-bignums"></a><h2>uniqnum() on oversized bignums</h2>
<p>Due to the way that <code class="inline"><span class="i">uniqnum</span><span class="s">(</span><span class="s">)</span></code>
 compares numbers, it cannot distinguish
differences between bignums (especially bigints) that are too large to fit in
the native platform types. For example,</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$x</span> = <span class="w">Math::BigInt</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="q">&quot;1&quot;</span> x <span class="n">100</span> <span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$y</span> = <span class="i">$x</span> + <span class="n">1</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="../functions/say.html">say</a> <a class="l_k" href="../functions/for.html">for</a> <span class="i">uniqnum</span><span class="s">(</span> <span class="i">$x</span><span class="cm">,</span> <span class="i">$y</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Will print just the value of <code class="inline"><span class="i">$x</span></code>
, believing that <code class="inline"><span class="i">$y</span></code>
 is a numerically-
equivalent value. This bug does not affect <code class="inline"><span class="i">uniqstr</span><span class="s">(</span><span class="s">)</span></code>
, which will correctly
observe that the two values stringify to different strings.</p>
<a name="SUGGESTED-ADDITIONS"></a><h1>SUGGESTED ADDITIONS</h1>
<p>The following are additions that have been requested, but I have been reluctant
to add due to them being very simple to implement in perl</p>
<pre class="verbatim"><ol><li>  <span class="c"># How many elements are true</span></li><li></li><li><a name="true"></a>  sub <span class="m">true</span> <span class="s">{</span> <a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span> <span class="i">$_</span> <span class="s">}</span> <span class="i">@_</span> <span class="s">}</span></li><li></li><li>  <span class="c"># How many elements are false</span></li><li></li><li><a name="false"></a>  sub <span class="m">false</span> <span class="s">{</span> <a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span> !<span class="i">$_</span> <span class="s">}</span> <span class="i">@_</span> <span class="s">}</span></li></ol></pre><a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="../Scalar/Util.html">Scalar::Util</a>, <a href="http://search.cpan.org/perldoc/List::MoreUtils">List::MoreUtils</a></p>
<a name="COPYRIGHT"></a><h1>COPYRIGHT</h1>
<p>Copyright (c) 1997-2007 Graham Barr &lt;gbarr@pobox.com&gt;. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.</p>
<p>Recent additions and current maintenance by
Paul Evans, &lt;leonerd@leonerd.org.uk&gt;.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#LIST-REDUCTION-FUNCTIONS">LIST-REDUCTION FUNCTIONS</a><ul><li><a href="#reduce">reduce</a><li><a href="#any">any</a><li><a href="#all">all</a><li><a href="#none">none</a><li><a href="#notall">notall</a><li><a href="#first">first</a><li><a href="#max">max</a><li><a href="#maxstr">maxstr</a><li><a href="#min">min</a><li><a href="#minstr">minstr</a><li><a href="#product">product</a><li><a href="#sum">sum</a><li><a href="#sum0">sum0</a></ul><li><a href="#KEY%2fVALUE-PAIR-LIST-FUNCTIONS">KEY/VALUE PAIR LIST FUNCTIONS</a><ul><li><a href="#pairs">pairs</a><li><a href="#unpairs">unpairs</a><li><a href="#pairkeys">pairkeys</a><li><a href="#pairvalues">pairvalues</a><li><a href="#pairgrep">pairgrep</a><li><a href="#pairfirst">pairfirst</a><li><a href="#pairmap">pairmap</a></ul><li><a href="#OTHER-FUNCTIONS">OTHER FUNCTIONS</a><ul><li><a href="#shuffle">shuffle</a><li><a href="#uniq">uniq</a><li><a href="#uniqnum">uniqnum</a><li><a href="#uniqstr">uniqstr</a></ul><li><a href="#KNOWN-BUGS">KNOWN BUGS</a><ul><li><a href="#RT-%2395409">RT #95409</a><li><a href="#uniqnum()-on-oversized-bignums">uniqnum() on oversized bignums</a></ul><li><a href="#SUGGESTED-ADDITIONS">SUGGESTED ADDITIONS</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#COPYRIGHT">COPYRIGHT</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="../index-overview.html">Overview</a>
                <li><a href="../index-tutorials.html">Tutorials</a>
                <li><a href="../index-faq.html">FAQs</a>
                <li><a href="../index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="../index-language.html">Language</a>
                <li><a href="../index-functions.html">Functions</a>
                <li><a href="../perlop.html">Operators</a>
                <li><a href="../perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="../index-modules-A.html">Modules</a>
                <li><a href="../index-pragmas.html">Pragmas</a>
                <li><a href="../index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="../index-licence.html">License</a>
                <li><a href="../index-internals.html">Internals</a>
                <li><a href="../index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="../static/exploreperl.js"></script>
      <script language="JavaScript" src="../static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(1);
  perldoc.pageName    = 'List::Util';
  perldoc.pageAddress = 'List/Util.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>