File: perlfaq4.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 (1736 lines) | stat: -rw-r--r-- 291,971 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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlfaq4 - 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>perlfaq4</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-faq.html">FAQs</a> &gt;
      
    
    perlfaq4
  

            </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>perlfaq4</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#VERSION">VERSION</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Data%3a-Numbers">Data: Numbers</a><ul><li><a href="#Why-am-I-getting-long-decimals-(eg%2c-19.9499999999999)-instead-of-the-numbers-I-should-be-getting-(eg%2c-19.95)%3f">Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?</a><li><a href="#Why-is-int()-broken%3f">Why is int() broken?</a><li><a href="#Why-isn't-my-octal-data-interpreted-correctly%3f">Why isn't my octal data interpreted correctly?</a><li><a href="#Does-Perl-have-a-round()-function%3f-What-about-ceil()-and-floor()%3f-Trig-functions%3f">Does Perl have a round() function? What about ceil() and floor()? Trig functions?</a><li><a href="#How-do-I-convert-between-numeric-representations%2fbases%2fradixes%3f">How do I convert between numeric representations/bases/radixes?</a><li><a href="#Why-doesn't-%26-work-the-way-I-want-it-to%3f">Why doesn't & work the way I want it to?</a><li><a href="#How-do-I-multiply-matrices%3f">How do I multiply matrices?</a><li><a href="#How-do-I-perform-an-operation-on-a-series-of-integers%3f">How do I perform an operation on a series of integers?</a><li><a href="#How-can-I-output-Roman-numerals%3f">How can I output Roman numerals?</a><li><a href="#Why-aren't-my-random-numbers-random%3f">Why aren't my random numbers random?</a><li><a href="#How-do-I-get-a-random-number-between-X-and-Y%3f">How do I get a random number between X and Y?</a></ul><li><a href="#Data%3a-Dates">Data: Dates</a><ul><li><a href="#How-do-I-find-the-day-or-week-of-the-year%3f">How do I find the day or week of the year?</a><li><a href="#How-do-I-find-the-current-century-or-millennium%3f">How do I find the current century or millennium?</a><li><a href="#How-can-I-compare-two-dates-and-find-the-difference%3f">How can I compare two dates and find the difference?</a><li><a href="#How-can-I-take-a-string-and-turn-it-into-epoch-seconds%3f">How can I take a string and turn it into epoch seconds?</a><li><a href="#How-can-I-find-the-Julian-Day%3f">How can I find the Julian Day?</a><li><a href="#How-do-I-find-yesterday's-date%3f">How do I find yesterday's date?
    
   
</a><li><a href="#Does-Perl-have-a-Year-2000-or-2038-problem%3f-Is-Perl-Y2K-compliant%3f">Does Perl have a Year 2000 or 2038 problem? Is Perl Y2K compliant?</a></ul><li><a href="#Data%3a-Strings">Data: Strings</a><ul><li><a href="#How-do-I-validate-input%3f">How do I validate input?</a><li><a href="#How-do-I-unescape-a-string%3f">How do I unescape a string?</a><li><a href="#How-do-I-remove-consecutive-pairs-of-characters%3f">How do I remove consecutive pairs of characters?</a><li><a href="#How-do-I-expand-function-calls-in-a-string%3f">How do I expand function calls in a string?</a><li><a href="#How-do-I-find-matching%2fnesting-anything%3f">How do I find matching/nesting anything?</a><li><a href="#How-do-I-reverse-a-string%3f">How do I reverse a string?</a><li><a href="#How-do-I-expand-tabs-in-a-string%3f">How do I expand tabs in a string?</a><li><a href="#How-do-I-reformat-a-paragraph%3f">How do I reformat a paragraph?</a><li><a href="#How-can-I-access-or-change-N-characters-of-a-string%3f">How can I access or change N characters of a string?</a><li><a href="#How-do-I-change-the-Nth-occurrence-of-something%3f">How do I change the Nth occurrence of something?</a><li><a href="#How-can-I-count-the-number-of-occurrences-of-a-substring-within-a-string%3f">How can I count the number of occurrences of a substring within a string?</a><li><a href="#How-do-I-capitalize-all-the-words-on-one-line%3f">How do I capitalize all the words on one line?
   </a><li><a href="#How-can-I-split-a-%5bcharacter%5d-delimited-string-except-when-inside-%5bcharacter%5d%3f">How can I split a [character]-delimited string except when inside [character]?</a><li><a href="#How-do-I-strip-blank-space-from-the-beginning%2fend-of-a-string%3f">How do I strip blank space from the beginning/end of a string?</a><li><a href="#How-do-I-pad-a-string-with-blanks-or-pad-a-number-with-zeroes%3f">How do I pad a string with blanks or pad a number with zeroes?</a><li><a href="#How-do-I-extract-selected-columns-from-a-string%3f">How do I extract selected columns from a string?</a><li><a href="#How-do-I-find-the-soundex-value-of-a-string%3f">How do I find the soundex value of a string?</a><li><a href="#How-can-I-expand-variables-in-text-strings%3f">How can I expand variables in text strings?</a><li><a href="#What's-wrong-with-always-quoting-%22%24vars%22%3f">What's wrong with always quoting "$vars"?</a><li><a href="#Why-don't-my-%3c%3cHERE-documents-work%3f">Why don't my <<HERE documents work?</a></ul><li><a href="#Data%3a-Arrays">Data: Arrays</a><ul><li><a href="#What-is-the-difference-between-a-list-and-an-array%3f">What is the difference between a list and an array?</a><li><a href="#What-is-the-difference-between-%24array%5b1%5d-and-%40array%5b1%5d%3f">What is the difference between $array[1] and @array[1]?</a><li><a href="#How-can-I-remove-duplicate-elements-from-a-list-or-array%3f">How can I remove duplicate elements from a list or array?</a><li><a href="#How-can-I-tell-whether-a-certain-element-is-contained-in-a-list-or-array%3f">How can I tell whether a certain element is contained in a list or array?</a><li><a href="#How-do-I-compute-the-difference-of-two-arrays%3f-How-do-I-compute-the-intersection-of-two-arrays%3f">How do I compute the difference of two arrays? How do I compute the intersection of two arrays?</a><li><a href="#How-do-I-test-whether-two-arrays-or-hashes-are-equal%3f">How do I test whether two arrays or hashes are equal?</a><li><a href="#How-do-I-find-the-first-array-element-for-which-a-condition-is-true%3f">How do I find the first array element for which a condition is true?</a><li><a href="#How-do-I-handle-linked-lists%3f">How do I handle linked lists?</a><li><a href="#How-do-I-handle-circular-lists%3f">How do I handle circular lists?
   
 </a><li><a href="#How-do-I-shuffle-an-array-randomly%3f">How do I shuffle an array randomly?</a><li><a href="#How-do-I-process%2fmodify-each-element-of-an-array%3f">How do I process/modify each element of an array?</a><li><a href="#How-do-I-select-a-random-element-from-an-array%3f">How do I select a random element from an array?</a><li><a href="#How-do-I-permute-N-elements-of-a-list%3f">How do I permute N elements of a list?
   
 </a><li><a href="#How-do-I-sort-an-array-by-(anything)%3f">How do I sort an array by (anything)?</a><li><a href="#How-do-I-manipulate-arrays-of-bits%3f">How do I manipulate arrays of bits?</a><li><a href="#Why-does-defined()-return-true-on-empty-arrays-and-hashes%3f">Why does defined() return true on empty arrays and hashes?</a></ul><li><a href="#Data%3a-Hashes-(Associative-Arrays)">Data: Hashes (Associative Arrays)</a><ul><li><a href="#How-do-I-process-an-entire-hash%3f">How do I process an entire hash?</a><li><a href="#How-do-I-merge-two-hashes%3f">How do I merge two hashes?
  </a><li><a href="#What-happens-if-I-add-or-remove-keys-from-a-hash-while-iterating-over-it%3f">What happens if I add or remove keys from a hash while iterating over it?</a><li><a href="#How-do-I-look-up-a-hash-element-by-value%3f">How do I look up a hash element by value?</a><li><a href="#How-can-I-know-how-many-entries-are-in-a-hash%3f">How can I know how many entries are in a hash?</a><li><a href="#How-do-I-sort-a-hash-(optionally-by-value-instead-of-key)%3f">How do I sort a hash (optionally by value instead of key)?</a><li><a href="#How-can-I-always-keep-my-hash-sorted%3f">How can I always keep my hash sorted?
</a><li><a href="#What's-the-difference-between-%22delete%22-and-%22undef%22-with-hashes%3f">What's the difference between "delete" and "undef" with hashes?</a><li><a href="#Why-don't-my-tied-hashes-make-the-defined%2fexists-distinction%3f">Why don't my tied hashes make the defined/exists distinction?</a><li><a href="#How-do-I-reset-an-each()-operation-part-way-through%3f">How do I reset an each() operation part-way through?</a><li><a href="#How-can-I-get-the-unique-keys-from-two-hashes%3f">How can I get the unique keys from two hashes?</a><li><a href="#How-can-I-store-a-multidimensional-array-in-a-DBM-file%3f">How can I store a multidimensional array in a DBM file?</a><li><a href="#How-can-I-make-my-hash-remember-the-order-I-put-elements-into-it%3f">How can I make my hash remember the order I put elements into it?</a><li><a href="#Why-does-passing-a-subroutine-an-undefined-element-in-a-hash-create-it%3f">Why does passing a subroutine an undefined element in a hash create it?</a><li><a href="#How-can-I-make-the-Perl-equivalent-of-a-C-structure%2fC%2b%2b-class%2fhash-or-array-of-hashes-or-arrays%3f">How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?</a><li><a href="#How-can-I-use-a-reference-as-a-hash-key%3f">How can I use a reference as a hash key?</a><li><a href="#How-can-I-check-if-a-key-exists-in-a-multilevel-hash%3f">How can I check if a key exists in a multilevel hash?</a><li><a href="#How-can-I-prevent-addition-of-unwanted-keys-into-a-hash%3f">How can I prevent addition of unwanted keys into a hash?</a></ul><li><a href="#Data%3a-Misc">Data: Misc</a><ul><li><a href="#How-do-I-handle-binary-data-correctly%3f">How do I handle binary data correctly?</a><li><a href="#How-do-I-determine-whether-a-scalar-is-a-number%2fwhole%2finteger%2ffloat%3f">How do I determine whether a scalar is a number/whole/integer/float?</a><li><a href="#How-do-I-keep-persistent-data-across-program-calls%3f">How do I keep persistent data across program calls?</a><li><a href="#How-do-I-print-out-or-copy-a-recursive-data-structure%3f">How do I print out or copy a recursive data structure?</a><li><a href="#How-do-I-define-methods-for-every-class%2fobject%3f">How do I define methods for every class/object?</a><li><a href="#How-do-I-verify-a-credit-card-checksum%3f">How do I verify a credit card checksum?</a><li><a href="#How-do-I-pack-arrays-of-doubles-or-floats-for-XS-code%3f">How do I pack arrays of doubles or floats for XS code?</a></ul><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlfaq4 - Data Manipulation</p>
<a name="VERSION"></a><h1>VERSION</h1>
<p>version 5.021011</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This section of the FAQ answers questions related to manipulating
numbers, dates, strings, arrays, hashes, and miscellaneous data issues.</p>
<a name="Data%3a-Numbers"></a><h1>Data: Numbers</h1>
<a name="Why-am-I-getting-long-decimals-(eg%2c-19.9499999999999)-instead-of-the-numbers-I-should-be-getting-(eg%2c-19.95)%3f"></a><h2>Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?</h2>
<p>For the long explanation, see David Goldberg's "What Every Computer
Scientist Should Know About Floating-Point Arithmetic"
(<a href="http://web.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf">http://web.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf</a>).</p>
<p>Internally, your computer represents floating-point numbers in binary.
Digital (as in powers of two) computers cannot store all numbers
exactly. Some real numbers lose precision in the process. This is a
problem with how computers store numbers and affects all computer
languages, not just Perl.</p>
<p><a href="perlnumber.html">perlnumber</a> shows the gory details of number representations and
conversions.</p>
<p>To limit the number of decimal places in your numbers, you can use the
<code class="inline"><a class="l_k" href="functions/printf.html">printf</a></code> or <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code> function. See
<a href="perlop.html#Floating-point-Arithmetic">Floating-point Arithmetic in perlop</a> for more details.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;%.2f&quot;</span><span class="cm">,</span> <span class="n">10</span>/<span class="n">3</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$number</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a> <span class="q">&quot;%.2f&quot;</span><span class="cm">,</span> <span class="n">10</span>/<span class="n">3</span><span class="sc">;</span></li></ol></pre><a name="Why-is-int()-broken%3f"></a><h2>Why is int() broken?</h2>
<p>Your <code class="inline"><a class="l_k" href="functions/int.html">int()</a></code> is most probably working just fine. It's the numbers that
aren't quite what you think.</p>
<p>First, see the answer to "Why am I getting long decimals
(eg, 19.9499999999999) instead of the numbers I should be getting
(eg, 19.95)?".</p>
<p>For example, this</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="n">0.6</span>/<span class="n">0.2</span>-<span class="n">2</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>will in most computers print 0, not 1, because even such simple
numbers as 0.6 and 0.2 cannot be presented exactly by floating-point
numbers. What you think in the above as 'three' is really more like
2.9999999999999995559.</p>
<a name="Why-isn't-my-octal-data-interpreted-correctly%3f"></a><h2>Why isn't my octal data interpreted correctly?</h2>
<p>(contributed by brian d foy)</p>
<p>You're probably trying to convert a string to a number, which Perl only
converts as a decimal number. When Perl converts a string to a number, it
ignores leading spaces and zeroes, then assumes the rest of the digits
are in base 10:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <span class="q">&#39;0644&#39;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$string</span> + <span class="n">0</span><span class="sc">;</span>  <span class="c"># prints 644</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$string</span> + <span class="n">44</span><span class="sc">;</span> <span class="c"># prints 688, certainly not octal!</span></li></ol></pre><p>This problem usually involves one of the Perl built-ins that has the
same name a Unix command that uses octal numbers as arguments on the
command line. In this example, <code class="inline"><a class="l_k" href="functions/chmod.html">chmod</a></code> on the command line knows that
its first argument is octal because that's what it does:</p>
<pre class="verbatim"><ol><li>    <span class="i">%prompt</span>&gt; <a class="l_k" href="functions/chmod.html">chmod</a> <span class="n">644</span> <span class="w">file</span></li></ol></pre><p>If you want to use the same literal digits (644) in Perl, you have to tell
Perl to treat them as octal numbers either by prefixing the digits with
a <code class="inline"><span class="n">0</span></code>
 or using <code class="inline"><a class="l_k" href="functions/oct.html">oct</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/chmod.html">chmod</a><span class="s">(</span>     <span class="n">0644</span><span class="cm">,</span> <span class="i">$filename</span> <span class="s">)</span><span class="sc">;</span>  <span class="c"># right, has leading zero</span></li><li>    <a class="l_k" href="functions/chmod.html">chmod</a><span class="s">(</span> <a class="l_k" href="functions/oct.html">oct</a><span class="s">(</span><span class="n">644</span><span class="s">)</span><span class="cm">,</span> <span class="i">$filename</span> <span class="s">)</span><span class="sc">;</span>  <span class="c"># also correct</span></li></ol></pre><p>The problem comes in when you take your numbers from something that Perl
thinks is a string, such as a command line argument in <code class="inline"><span class="i">@ARGV</span></code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/chmod.html">chmod</a><span class="s">(</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="cm">,</span>      <span class="i">$filename</span> <span class="s">)</span><span class="sc">;</span>  <span class="c"># wrong, even if &quot;0644&quot;</span></li><li></li><li>    <a class="l_k" href="functions/chmod.html">chmod</a><span class="s">(</span> <a class="l_k" href="functions/oct.html">oct</a><span class="s">(</span><span class="i">$ARGV</span>[<span class="n">0</span>]<span class="s">)</span><span class="cm">,</span> <span class="i">$filename</span> <span class="s">)</span><span class="sc">;</span>  <span class="c"># correct, treat string as octal</span></li></ol></pre><p>You can always check the value you're using by printing it in octal
notation to ensure it matches what you think it should be. Print it
in octal  and decimal format:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;0%o %d&quot;</span><span class="cm">,</span> <span class="i">$number</span><span class="cm">,</span> <span class="i">$number</span><span class="sc">;</span></li></ol></pre><a name="Does-Perl-have-a-round()-function%3f-What-about-ceil()-and-floor()%3f-Trig-functions%3f"></a><h2>Does Perl have a round() function? What about ceil() and floor()? Trig functions?</h2>
<p>Remember that <code class="inline"><a class="l_k" href="functions/int.html">int()</a></code> merely truncates toward 0. For rounding to a
certain number of digits, <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf()</a></code> or <code class="inline"><a class="l_k" href="functions/printf.html">printf()</a></code> is usually the
easiest route.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/printf.html">printf</a><span class="s">(</span><span class="q">&quot;%.3f&quot;</span><span class="cm">,</span> <span class="n">3.1415926535</span><span class="s">)</span><span class="sc">;</span>   <span class="c"># prints 3.142</span></li></ol></pre><p>The <a href="POSIX.html">POSIX</a> module (part of the standard Perl distribution)
implements <code class="inline"><span class="i">ceil</span><span class="s">(</span><span class="s">)</span></code>
, <code class="inline"><span class="i">floor</span><span class="s">(</span><span class="s">)</span></code>
, and a number of other mathematical
and trigonometric functions.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">POSIX</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$ceil</span>   = <span class="i">ceil</span><span class="s">(</span><span class="n">3.5</span><span class="s">)</span><span class="sc">;</span>   <span class="c"># 4</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$floor</span>  = <span class="i">floor</span><span class="s">(</span><span class="n">3.5</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># 3</span></li></ol></pre><p>In 5.000 to 5.003 perls, trigonometry was done in the <a href="Math/Complex.html">Math::Complex</a>
module. With 5.004, the <a href="Math/Trig.html">Math::Trig</a> module (part of the standard Perl
distribution) implements the trigonometric functions. Internally it
uses the <a href="Math/Complex.html">Math::Complex</a> module and some functions can break out from
the real axis into the complex plane, for example the inverse sine of
2.</p>
<p>Rounding in financial applications can have serious implications, and
the rounding method used should be specified precisely. In these
cases, it probably pays not to trust whichever system of rounding is
being used by Perl, but instead to implement the rounding function you
need yourself.</p>
<p>To see why, notice how you'll still have an issue on half-way-point
alternation:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$i</span> &lt; <span class="n">1.01</span><span class="sc">;</span> <span class="i">$i</span> += <span class="n">0.05</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;%.1f &quot;</span><span class="cm">,</span><span class="i">$i</span><span class="s">}</span></li><li></li><li>    <span class="n">0.0</span> <span class="n">0.1</span> <span class="n">0.1</span> <span class="n">0.2</span> <span class="n">0.2</span> <span class="n">0.2</span> <span class="n">0.3</span> <span class="n">0.3</span> <span class="n">0.4</span> <span class="n">0.4</span> <span class="n">0.5</span> <span class="n">0.5</span> <span class="n">0.6</span> <span class="n">0.7</span> <span class="n">0.7</span></li><li>    <span class="n">0.8</span> <span class="n">0.8</span> <span class="n">0.9</span> <span class="n">0.9</span> <span class="n">1.0</span> <span class="n">1.0</span></li></ol></pre><p>Don't blame Perl. It's the same as in C. IEEE says we have to do
this. Perl numbers whose absolute values are integers under 2**31 (on
32-bit machines) will work pretty much like mathematical integers.
Other numbers are not guaranteed.</p>
<a name="How-do-I-convert-between-numeric-representations%2fbases%2fradixes%3f"></a><h2>How do I convert between numeric representations/bases/radixes?</h2>
<p>As always with Perl there is more than one way to do it. Below are a
few examples of approaches to making common conversions between number
representations. This is intended to be representational rather than
exhaustive.</p>
<p>Some of the examples later in <a href="perlfaq4.html">perlfaq4</a> use the <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a>
module from CPAN. The reason you might choose <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a> over the
perl built-in functions is that it works with numbers of ANY size,
that it is optimized for speed on some operations, and for at least
some programmers the notation might be familiar.</p>
<ul>
<li><a name="How-do-I-convert-hexadecimal-into-decimal"></a><b>How do I convert hexadecimal into decimal</b>
<p>Using perl's built in conversion of <code class="inline"><span class="n">0</span>x</code>
 notation:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <span class="n">0xDEADBEEF</span><span class="sc">;</span></li></ol></pre><p>Using the <code class="inline"><a class="l_k" href="functions/hex.html">hex</a></code> function:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <a class="l_k" href="functions/hex.html">hex</a><span class="s">(</span><span class="q">&quot;DEADBEEF&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/pack.html">pack</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;N&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;H8&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span><span class="q">&quot;0&quot;</span> x <span class="n">8</span> . <span class="q">&quot;DEADBEEF&quot;</span><span class="cm">,</span> <span class="n">-8</span><span class="s">)</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using the CPAN module <code class="inline"><span class="w">Bit::Vector</span></code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Bit::Vector</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new_Hex</span><span class="s">(</span><span class="n">32</span><span class="cm">,</span> <span class="q">&quot;DEADBEEF&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <span class="i">$vec</span><span class="i">-&gt;to_Dec</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="How-do-I-convert-from-decimal-to-hexadecimal"></a><b>How do I convert from decimal to hexadecimal</b>
<p>Using <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$hex</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%X&quot;</span><span class="cm">,</span> <span class="n">3735928559</span><span class="s">)</span><span class="sc">;</span> <span class="c"># upper case A-F</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$hex</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%x&quot;</span><span class="cm">,</span> <span class="n">3735928559</span><span class="s">)</span><span class="sc">;</span> <span class="c"># lower case a-f</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/unpack.html">unpack</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$hex</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;H*&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;N&quot;</span><span class="cm">,</span> <span class="n">3735928559</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Bit::Vector</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new_Dec</span><span class="s">(</span><span class="n">32</span><span class="cm">,</span> <span class="n">-559038737</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$hex</span> = <span class="i">$vec</span><span class="i">-&gt;to_Hex</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>And <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a> supports odd bit counts:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Bit::Vector</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new_Dec</span><span class="s">(</span><span class="n">33</span><span class="cm">,</span> <span class="n">3735928559</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$vec</span><span class="i">-&gt;Resize</span><span class="s">(</span><span class="n">32</span><span class="s">)</span><span class="sc">;</span> <span class="c"># suppress leading 0 if unwanted</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$hex</span> = <span class="i">$vec</span><span class="i">-&gt;to_Hex</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="How-do-I-convert-from-octal-to-decimal"></a><b>How do I convert from octal to decimal</b>
<p>Using Perl's built in conversion of numbers with leading zeros:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <span class="n">033653337357</span><span class="sc">;</span> <span class="c"># note the leading 0!</span></li></ol></pre><p>Using the <code class="inline"><a class="l_k" href="functions/oct.html">oct</a></code> function:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <a class="l_k" href="functions/oct.html">oct</a><span class="s">(</span><span class="q">&quot;33653337357&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Bit::Vector</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">32</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$vec</span><span class="i">-&gt;Chunk_List_Store</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span> <a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span> <a class="l_k" href="functions/reverse.html">reverse</a> <span class="q">&quot;33653337357&quot;</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <span class="i">$vec</span><span class="i">-&gt;to_Dec</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="How-do-I-convert-from-decimal-to-octal"></a><b>How do I convert from decimal to octal</b>
<p>Using <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$oct</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%o&quot;</span><span class="cm">,</span> <span class="n">3735928559</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Bit::Vector</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new_Dec</span><span class="s">(</span><span class="n">32</span><span class="cm">,</span> <span class="n">-559038737</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$oct</span> = <a class="l_k" href="functions/reverse.html">reverse</a> <a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">&#39;&#39;</span><span class="cm">,</span> <span class="i">$vec</span><span class="i">-&gt;Chunk_List_Read</span><span class="s">(</span><span class="n">3</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="How-do-I-convert-from-binary-to-decimal"></a><b>How do I convert from binary to decimal</b>
<p>Perl 5.6 lets you write binary numbers directly with
the <code class="inline">0b</code> notation:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$number</span> = <span class="n">0b10110110</span><span class="sc">;</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/oct.html">oct</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$input</span> = <span class="q">&quot;10110110&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$decimal</span> = <a class="l_k" href="functions/oct.html">oct</a><span class="s">(</span> <span class="q">&quot;0b$input&quot;</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/pack.html">pack</a></code> and <code class="inline"><a class="l_k" href="functions/ord.html">ord</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$decimal</span> = <a class="l_k" href="functions/ord.html">ord</a><span class="s">(</span><a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">&#39;B8&#39;</span><span class="cm">,</span> <span class="q">&#39;10110110&#39;</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/pack.html">pack</a></code> and <code class="inline"><a class="l_k" href="functions/unpack.html">unpack</a></code> for larger strings:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$int</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;N&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;B32&quot;</span><span class="cm">,</span></li><li>    <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span><span class="q">&quot;0&quot;</span> x <span class="n">32</span> . <span class="q">&quot;11110101011011011111011101111&quot;</span><span class="cm">,</span> <span class="n">-32</span><span class="s">)</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%d&quot;</span><span class="cm">,</span> <span class="i">$int</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># substr() is used to left-pad a 32-character string with zeros.</span></li></ol></pre><p>Using <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new_Bin</span><span class="s">(</span><span class="n">32</span><span class="cm">,</span> <span class="q">&quot;11011110101011011011111011101111&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dec</span> = <span class="i">$vec</span><span class="i">-&gt;to_Dec</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="How-do-I-convert-from-decimal-to-binary"></a><b>How do I convert from decimal to binary</b>
<p>Using <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code> (perl 5.6+):</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$bin</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%b&quot;</span><span class="cm">,</span> <span class="n">3735928559</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/unpack.html">unpack</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$bin</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;B*&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;N&quot;</span><span class="cm">,</span> <span class="n">3735928559</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Using <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Bit::Vector</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new_Dec</span><span class="s">(</span><span class="n">32</span><span class="cm">,</span> <span class="n">-559038737</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$bin</span> = <span class="i">$vec</span><span class="i">-&gt;to_Bin</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The remaining transformations (e.g. hex -&gt; oct, bin -&gt; hex, etc.)
are left as an exercise to the inclined reader.</p>
</li>
</ul>
<a name="Why-doesn't-%26-work-the-way-I-want-it-to%3f"></a><h2>Why doesn't &amp; work the way I want it to?</h2>
<p>The behavior of binary arithmetic operators depends on whether they're
used on numbers or strings. The operators treat a string as a series
of bits and work with that (the string <code class="inline"><span class="q">&quot;3&quot;</span></code>
 is the bit pattern
<code class="inline"><span class="n">00110011</span></code>
). The operators work with the binary form of a number
(the number <code class="inline"><span class="n">3</span></code>
 is treated as the bit pattern <code class="inline"><span class="n">00000011</span></code>
).</p>
<p>So, saying <code class="inline"><span class="n">11</span> &amp; <span class="n">3</span></code>
 performs the "and" operation on numbers (yielding
<code class="inline"><span class="n">3</span></code>
). Saying <code class="inline"><span class="q">&quot;11&quot;</span> &amp; <span class="q">&quot;3&quot;</span></code>
 performs the "and" operation on strings
(yielding <code class="inline"><span class="q">&quot;1&quot;</span></code>
).</p>
<p>Most problems with <code class="inline"><span class="i">&amp;</span></code>
 and <code class="inline">|</code> arise because the programmer thinks
they have a number but really it's a string or vice versa. To avoid this,
stringify the arguments explicitly (using <code class="inline"><span class="q">&quot;&quot;</span></code>
 or <code class="inline"><a class="l_k" href="functions/qq.html">qq()</a></code>) or convert them
to numbers explicitly (using <code class="inline"><span class="n">0</span>+<span class="i">$arg</span></code>
). The rest arise because
the programmer says:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">&quot;\020\020&quot;</span> &amp; <span class="q">&quot;\101\101&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># ...</span></li><li>    <span class="s">}</span></li></ol></pre><p>but a string consisting of two null bytes (the result of <code class="inline"><span class="q">&quot;\020\020&quot;</span>
&amp; <span class="q">&quot;\101\101&quot;</span></code>
) is not a false value in Perl. You need:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="s">(</span><span class="q">&quot;\020\020&quot;</span> &amp; <span class="q">&quot;\101\101&quot;</span><span class="s">)</span> !~ <span class="q">/[^\000]/</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># ...</span></li><li>    <span class="s">}</span></li></ol></pre><a name="How-do-I-multiply-matrices%3f"></a><h2>How do I multiply matrices?</h2>
<p>Use the <a href="http://search.cpan.org/perldoc/Math::Matrix">Math::Matrix</a> or <a href="http://search.cpan.org/perldoc/Math::MatrixReal">Math::MatrixReal</a> modules (available from CPAN)
or the <a href="http://search.cpan.org/perldoc/PDL">PDL</a> extension (also available from CPAN).</p>
<a name="How-do-I-perform-an-operation-on-a-series-of-integers%3f"></a><h2>How do I perform an operation on a series of integers?</h2>
<p>To call a function on each element in an array, and collect the
results, use:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@results</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">my_func</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span> <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@triple</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="n">3</span> * <span class="i">$_</span> <span class="s">}</span> <span class="i">@single</span><span class="sc">;</span></li></ol></pre><p>To call a function on each element of an array, but ignore the
results:</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">$iterator</span> <span class="s">(</span><span class="i">@array</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">some_func</span><span class="s">(</span><span class="i">$iterator</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>To call a function on each integer in a (small) range, you <b>can</b> use:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@results</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">some_func</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span> <span class="s">}</span> <span class="s">(</span><span class="n">5</span> .. <span class="n">25</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>but you should be aware that in this form, the <code class="inline">..</code>
 operator
creates a list of all integers in the range, which can take a lot of
memory for large ranges. However, the problem does not occur when
using <code class="inline">..</code>
 within a <code class="inline">for</code>
 loop, because in that case the range
operator is optimized to <i>iterate</i> over the range, without creating
the entire list. So</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@results</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> <span class="s">(</span><span class="n">5</span> .. <span class="n">500_005</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@results</span><span class="cm">,</span> <span class="i">some_func</span><span class="s">(</span><span class="i">$i</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>or even</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@results</span><span class="cm">,</span> <span class="i">some_func</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="s">)</span> <a class="l_k" href="functions/for.html">for</a> <span class="n">5</span> .. <span class="n">500_005</span><span class="sc">;</span></li></ol></pre><p>will not create an intermediate list of 500,000 integers.</p>
<a name="How-can-I-output-Roman-numerals%3f"></a><h2>How can I output Roman numerals?</h2>
<p>Get the <a href="http://www.cpan.org/modules/by-module/Roman">http://www.cpan.org/modules/by-module/Roman</a> module.</p>
<a name="Why-aren't-my-random-numbers-random%3f"></a><h2>Why aren't my random numbers random?</h2>
<p>If you're using a version of Perl before 5.004, you must call <code class="inline"><a class="l_k" href="functions/srand.html">srand</a></code>
once at the start of your program to seed the random number generator.</p>
<pre class="verbatim"><ol><li>     <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <a class="l_k" href="functions/srand.html">srand</a><span class="s">(</span><span class="s">)</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$]</span> &lt; <span class="n">5.004</span> <span class="s">}</span></li></ol></pre><p>5.004 and later automatically call <code class="inline"><a class="l_k" href="functions/srand.html">srand</a></code> at the beginning. Don't
call <code class="inline"><a class="l_k" href="functions/srand.html">srand</a></code> more than once--you make your numbers less random,
rather than more.</p>
<p>Computers are good at being predictable and bad at being random
(despite appearances caused by bugs in your programs :-). The
<i>random</i> article in the "Far More Than You Ever Wanted To Know"
collection in <a href="http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz">http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz</a>, courtesy
of Tom Phoenix, talks more about this. John von Neumann said, "Anyone
who attempts to generate random numbers by deterministic means is, of
course, living in a state of sin."</p>
<p>Perl relies on the underlying system for the implementation of
<code class="inline"><a class="l_k" href="functions/rand.html">rand</a></code> and <code class="inline"><a class="l_k" href="functions/srand.html">srand</a></code>; on some systems, the generated numbers are
not random enough (especially on Windows : see
<a href="http://www.perlmonks.org/?node_id=803632">http://www.perlmonks.org/?node_id=803632</a>).
Several CPAN modules in the <code class="inline"><span class="w">Math</span></code>
 namespace implement better
pseudorandom generators; see for example
<a href="http://search.cpan.org/perldoc/Math::Random::MT">Math::Random::MT</a> ("Mersenne Twister", fast), or
<a href="http://search.cpan.org/perldoc/Math::TrulyRandom">Math::TrulyRandom</a> (uses the imperfections in the system's
timer to generate random numbers, which is rather slow).
More algorithms for random numbers are described in
"Numerical Recipes in C" at <a href="http://www.nr.com/">http://www.nr.com/</a></p>
<a name="How-do-I-get-a-random-number-between-X-and-Y%3f"></a><h2>How do I get a random number between X and Y?</h2>
<p>To get a random number between two values, you can use the <code class="inline"><a class="l_k" href="functions/rand.html">rand()</a></code>
built-in to get a random number between 0 and 1. From there, you shift
that into the range that you want.</p>
<p><code class="inline"><a class="l_k" href="functions/rand.html">rand($x)</a></code> returns a number such that <code class="inline"><span class="n">0</span> &lt;= <a class="l_k" href="functions/rand.html">rand</a><span class="s">(</span><span class="i">$x</span><span class="s">)</span> &lt; <span class="i">$x</span></code>
. Thus
what you want to have perl figure out is a random number in the range
from 0 to the difference between your <i>X</i> and <i>Y</i>.</p>
<p>That is, to get a number between 10 and 15, inclusive, you want a
random number between 0 and 5 that you can then add to 10.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$number</span> = <span class="n">10</span> + <a class="l_k" href="functions/int.html">int</a> <a class="l_k" href="functions/rand.html">rand</a><span class="s">(</span> <span class="n">15</span>-<span class="n">10</span>+<span class="n">1</span> <span class="s">)</span><span class="sc">;</span> <span class="c"># ( 10,11,12,13,14, or 15 )</span></li></ol></pre><p>Hence you derive the following simple function to abstract
that. It selects a random integer between the two given
integers (inclusive). For example: <code class="inline"><span class="i">random_int_between</span><span class="s">(</span><span class="n">50</span><span class="cm">,</span><span class="n">120</span><span class="s">)</span></code>
.</p>
<pre class="verbatim"><ol><li><a name="random_int_between"></a>    sub <span class="m">random_int_between</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$min</span><span class="cm">,</span> <span class="i">$max</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>        <span class="c"># Assumes that the two arguments are integers themselves!</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="i">$min</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$min</span> == <span class="i">$max</span><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$min</span><span class="cm">,</span> <span class="i">$max</span><span class="s">)</span> = <span class="s">(</span><span class="i">$max</span><span class="cm">,</span> <span class="i">$min</span><span class="s">)</span>  <a class="l_k" href="functions/if.html">if</a>  <span class="i">$min</span> &gt; <span class="i">$max</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="i">$min</span> + <a class="l_k" href="functions/int.html">int</a> <a class="l_k" href="functions/rand.html">rand</a><span class="s">(</span><span class="n">1</span> + <span class="i">$max</span> - <span class="i">$min</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><a name="Data%3a-Dates"></a><h1>Data: Dates</h1>
<a name="How-do-I-find-the-day-or-week-of-the-year%3f"></a><h2>How do I find the day or week of the year?</h2>
<p>The day of the year is in the list returned
by the <code class="inline"><a class="l_k" href="functions/localtime.html">localtime</a></code> function. Without an
argument <code class="inline"><a class="l_k" href="functions/localtime.html">localtime</a></code> uses the current time.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$day_of_year</span> = <span class="s">(</span><a class="l_k" href="functions/localtime.html">localtime</a><span class="s">)</span>[<span class="n">7</span>]<span class="sc">;</span></li></ol></pre><p>The <a href="POSIX.html">POSIX</a> module can also format a date as the day of the year or
week of the year.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw/strftime/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$day_of_year</span>  = <span class="w">strftime</span> <span class="q">&quot;%j&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/localtime.html">localtime</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$week_of_year</span> = <span class="w">strftime</span> <span class="q">&quot;%W&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/localtime.html">localtime</a><span class="sc">;</span></li></ol></pre><p>To get the day of year for any date, use <a href="POSIX.html">POSIX</a>'s <code class="inline"><span class="w">mktime</span></code>
 to get
a time in epoch seconds for the argument to <code class="inline"><a class="l_k" href="functions/localtime.html">localtime</a></code>.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw/mktime strftime/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$week_of_year</span> = <span class="w">strftime</span> <span class="q">&quot;%W&quot;</span><span class="cm">,</span></li><li>        <a class="l_k" href="functions/localtime.html">localtime</a><span class="s">(</span> <span class="i">mktime</span><span class="s">(</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">18</span><span class="cm">,</span> <span class="n">11</span><span class="cm">,</span> <span class="n">87</span> <span class="s">)</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can also use <a href="Time/Piece.html">Time::Piece</a>, which comes with Perl and provides a
<code class="inline"><a class="l_k" href="functions/localtime.html">localtime</a></code> that returns an object:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Time::Piece</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$day_of_year</span>  = <a class="l_k" href="functions/localtime.html">localtime</a><span class="i">-&gt;yday</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$week_of_year</span> = <a class="l_k" href="functions/localtime.html">localtime</a><span class="i">-&gt;week</span><span class="sc">;</span></li></ol></pre><p>The <a href="http://search.cpan.org/perldoc/Date::Calc">Date::Calc</a> module provides two functions to calculate these, too:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Date::Calc</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$day_of_year</span>  = <span class="i">Day_of_Year</span><span class="s">(</span>  <span class="n">1987</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">18</span> <span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$week_of_year</span> = <span class="i">Week_of_Year</span><span class="s">(</span> <span class="n">1987</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">18</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-find-the-current-century-or-millennium%3f"></a><h2>How do I find the current century or millennium?</h2>
<p>Use the following simple functions:</p>
<pre class="verbatim"><ol><li><a name="get_century"></a>    sub <span class="m">get_century</span>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="s">(</span><span class="s">(</span><span class="s">(</span><a class="l_k" href="functions/localtime.html">localtime</a><span class="s">(</span><a class="l_k" href="functions/shift.html">shift</a> || <a class="l_k" href="functions/time.html">time</a><span class="s">)</span><span class="s">)</span>[<span class="n">5</span>] + <span class="n">1999</span><span class="s">)</span><span class="s">)</span>/<span class="n">100</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="get_millennium"></a>    sub <span class="m">get_millennium</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="n">1</span>+<a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="s">(</span><span class="s">(</span><span class="s">(</span><a class="l_k" href="functions/localtime.html">localtime</a><span class="s">(</span><a class="l_k" href="functions/shift.html">shift</a> || <a class="l_k" href="functions/time.html">time</a><span class="s">)</span><span class="s">)</span>[<span class="n">5</span>] + <span class="n">1899</span><span class="s">)</span><span class="s">)</span>/<span class="n">1000</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>On some systems, the <a href="POSIX.html">POSIX</a> module's <code class="inline"><span class="i">strftime</span><span class="s">(</span><span class="s">)</span></code>
 function has been
extended in a non-standard way to use a <code class="inline"><span class="i">%C</span></code>
 format, which they
sometimes claim is the "century". It isn't, because on most such
systems, this is only the first two digits of the four-digit year, and
thus cannot be used to determine reliably the current century or
millennium.</p>
<a name="How-can-I-compare-two-dates-and-find-the-difference%3f"></a><h2>How can I compare two dates and find the difference?</h2>
<p>(contributed by brian d foy)</p>
<p>You could just store all your dates as a number and then subtract.
Life isn't always that simple though.</p>
<p>The <a href="Time/Piece.html">Time::Piece</a> module, which comes with Perl, replaces <a href="functions/localtime.html">localtime</a>
with a version that returns an object. It also overloads the comparison
operators so you can compare them directly:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Time::Piece</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$date1</span> = <a class="l_k" href="functions/localtime.html">localtime</a><span class="s">(</span> <span class="i">$some_time</span> <span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$date2</span> = <a class="l_k" href="functions/localtime.html">localtime</a><span class="s">(</span> <span class="i">$some_other_time</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="i">$date1</span> &lt; <span class="i">$date2</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The date was in the past\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>You can also get differences with a subtraction, which returns a
<a href="Time/Seconds.html">Time::Seconds</a> object:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$date_diff</span> = <span class="i">$date1</span> - <span class="i">$date2</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The difference is &quot;</span><span class="cm">,</span> <span class="i">$date_diff</span><span class="i">-&gt;days</span><span class="cm">,</span> <span class="q">&quot; days\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you want to work with formatted dates, the <a href="http://search.cpan.org/perldoc/Date::Manip">Date::Manip</a>,
<a href="http://search.cpan.org/perldoc/Date::Calc">Date::Calc</a>, or <a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> modules can help you.</p>
<a name="How-can-I-take-a-string-and-turn-it-into-epoch-seconds%3f"></a><h2>How can I take a string and turn it into epoch seconds?</h2>
<p>If it's a regular enough string that it always has the same format,
you can split it up and pass the parts to <code class="inline"><span class="w">timelocal</span></code>
 in the standard
<a href="Time/Local.html">Time::Local</a> module. Otherwise, you should look into the <a href="http://search.cpan.org/perldoc/Date::Calc">Date::Calc</a>,
<a href="http://search.cpan.org/perldoc/Date::Parse">Date::Parse</a>, and <a href="http://search.cpan.org/perldoc/Date::Manip">Date::Manip</a> modules from CPAN.</p>
<a name="How-can-I-find-the-Julian-Day%3f"></a><h2>How can I find the Julian Day?</h2>
<p>(contributed by brian d foy and Dave Cross)</p>
<p>You can use the <a href="Time/Piece.html">Time::Piece</a> module, part of the Standard Library,
which can convert a date/time to a Julian Day:</p>
<pre class="verbatim"><ol><li>    <span class="i">$ perl</span> -<span class="w">MTime::Piece</span> -<a class="l_k" href="functions/le.html">le</a> <span class="q">&#39;print localtime-&gt;julian_day&#39;</span></li><li>    <span class="n">2455607.7959375</span></li></ol></pre><p>Or the modified Julian Day:</p>
<pre class="verbatim"><ol><li>    <span class="i">$ perl</span> -<span class="w">MTime::Piece</span> -<a class="l_k" href="functions/le.html">le</a> <span class="q">&#39;print localtime-&gt;mjd&#39;</span></li><li>    <span class="n">55607.2961226851</span></li></ol></pre><p>Or even the day of the year (which is what some people think of as a
Julian day):</p>
<pre class="verbatim"><ol><li>    <span class="i">$ perl</span> -<span class="w">MTime::Piece</span> -<a class="l_k" href="functions/le.html">le</a> <span class="q">&#39;print localtime-&gt;yday&#39;</span></li><li>    <span class="n">45</span></li></ol></pre><p>You can also do the same things with the <a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> module:</p>
<pre class="verbatim"><ol><li>    <span class="i">$ perl</span> -<span class="w">MDateTime</span> -<a class="l_k" href="functions/le.html">le</a><span class="q">&#39;print DateTime-&gt;today-&gt;jd&#39;</span></li><li>    <span class="n">2453401.5</span></li><li>    <span class="i">$ perl</span> -<span class="w">MDateTime</span> -<a class="l_k" href="functions/le.html">le</a><span class="q">&#39;print DateTime-&gt;today-&gt;mjd&#39;</span></li><li>    <span class="n">53401</span></li><li>    <span class="i">$ perl</span> -<span class="w">MDateTime</span> -<a class="l_k" href="functions/le.html">le</a><span class="q">&#39;print DateTime-&gt;today-&gt;doy&#39;</span></li><li>    <span class="n">31</span></li></ol></pre><p>You can use the <a href="http://search.cpan.org/perldoc/Time::JulianDay">Time::JulianDay</a> module available on CPAN. Ensure
that you really want to find a Julian day, though, as many people have
different ideas about Julian days (see <a href="http://www.hermetic.ch/cal_stud/jdn.htm">http://www.hermetic.ch/cal_stud/jdn.htm</a>
for instance):</p>
<pre class="verbatim"><ol><li>    <span class="i">$  perl</span> -<span class="w">MTime::JulianDay</span> -<a class="l_k" href="functions/le.html">le</a> <span class="q">&#39;print local_julian_day( time )&#39;</span></li><li>    <span class="n">55608</span></li></ol></pre><a name="How-do-I-find-yesterday's-date%3f"></a><h2>How do I find yesterday's date?
    
   
</h2>
<p>(contributed by brian d foy)</p>
<p>To do it correctly, you can use one of the <code class="inline"><span class="w">Date</span></code>
 modules since they
work with calendars instead of times. The <a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> module makes it
simple, and give you the same time of day, only the day before,
despite daylight saving time changes:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">DateTime</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$yesterday</span> = <span class="w">DateTime</span><span class="w">-&gt;now</span><span class="w">-&gt;subtract</span><span class="s">(</span> <span class="w">days</span> <span class="cm">=&gt;</span> <span class="n">1</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Yesterday was $yesterday\n&quot;</span><span class="sc">;</span></li></ol></pre><p>You can also use the <a href="http://search.cpan.org/perldoc/Date::Calc">Date::Calc</a> module using its <code class="inline"><span class="w">Today_and_Now</span></code>

function.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Date::Calc</span> <span class="q">qw( Today_and_Now Add_Delta_DHMS )</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@date_time</span> = <span class="i">Add_Delta_DHMS</span><span class="s">(</span> <span class="i">Today_and_Now</span><span class="s">(</span><span class="s">)</span><span class="cm">,</span> <span class="n">-1</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;@date_time\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Most people try to use the time rather than the calendar to figure out
dates, but that assumes that days are twenty-four hours each. For
most people, there are two days a year when they aren't: the switch to
and from summer time throws this off. For example, the rest of the
suggestions will be wrong sometimes:</p>
<p>Starting with Perl 5.10, <a href="Time/Piece.html">Time::Piece</a> and <a href="Time/Seconds.html">Time::Seconds</a> are part
of the standard distribution, so you might think that you could do
something like this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Time::Piece</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Time::Seconds</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$yesterday</span> = <a class="l_k" href="functions/localtime.html">localtime</a><span class="s">(</span><span class="s">)</span> - <span class="w">ONE_DAY</span><span class="sc">;</span> <span class="c"># WRONG</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Yesterday was $yesterday\n&quot;</span><span class="sc">;</span></li></ol></pre><p>The <a href="Time/Piece.html">Time::Piece</a> module exports a new <code class="inline"><a class="l_k" href="functions/localtime.html">localtime</a></code> that returns an
object, and <a href="Time/Seconds.html">Time::Seconds</a> exports the <code class="inline"><span class="w">ONE_DAY</span></code>
 constant that is a
set number of seconds. This means that it always gives the time 24
hours ago, which is not always yesterday. This can cause problems
around the end of daylight saving time when there's one day that is 25
hours long.</p>
<p>You have the same problem with <a href="Time/Local.html">Time::Local</a>, which will give the wrong
answer for those same special cases:</p>
<pre class="verbatim"><ol><li>    <span class="c"># contributed by Gunnar Hjalmarsson</span></li><li>     <a class="l_k" href="functions/use.html">use</a> <span class="w">Time::Local</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$today</span> = <span class="w">timelocal</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="s">(</span> <a class="l_k" href="functions/localtime.html">localtime</a> <span class="s">)</span>[<span class="n">3</span>..<span class="n">5</span>]<span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$d</span><span class="cm">,</span> <span class="i">$m</span><span class="cm">,</span> <span class="i">$y</span><span class="s">)</span> = <span class="s">(</span> <a class="l_k" href="functions/localtime.html">localtime</a> <span class="i">$today</span>-<span class="n">86400</span> <span class="s">)</span>[<span class="n">3</span>..<span class="n">5</span>]<span class="sc">;</span> <span class="c"># WRONG</span></li><li>     <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;Yesterday: %d-%02d-%02d\n&quot;</span><span class="cm">,</span> <span class="i">$y</span>+<span class="n">1900</span><span class="cm">,</span> <span class="i">$m</span>+<span class="n">1</span><span class="cm">,</span> <span class="i">$d</span><span class="sc">;</span></li></ol></pre><a name="Does-Perl-have-a-Year-2000-or-2038-problem%3f-Is-Perl-Y2K-compliant%3f"></a><h2>Does Perl have a Year 2000 or 2038 problem? Is Perl Y2K compliant?</h2>
<p>(contributed by brian d foy)</p>
<p>Perl itself never had a Y2K problem, although that never stopped people
from creating Y2K problems on their own. See the documentation for
<code class="inline"><a class="l_k" href="functions/localtime.html">localtime</a></code> for its proper use.</p>
<p>Starting with Perl 5.12, <code class="inline"><a class="l_k" href="functions/localtime.html">localtime</a></code> and <code class="inline"><a class="l_k" href="functions/gmtime.html">gmtime</a></code> can handle dates past
03:14:08 January 19, 2038, when a 32-bit based time would overflow. You
still might get a warning on a 32-bit <code class="inline"><span class="w">perl</span></code>
:</p>
<pre class="verbatim"><ol><li>    <span class="i">% perl5</span>.<span class="n">12</span> -<span class="w">E</span> <span class="q">&#39;say scalar localtime( 0x9FFF_FFFFFFFF )&#39;</span></li><li>    <span class="w">Integer</span> <span class="w">overflow</span> <span class="w">in</span> <span class="w">hexadecimal</span> <span class="w">number</span> <span class="w">at</span> -e <span class="w">line</span> <span class="n">1.</span></li><li>    <span class="w">Wed</span> <span class="w">Nov</span>  <span class="n">1</span> <span class="n">19</span><span class="co">:</span><span class="n">42</span><span class="co">:</span><span class="n">39</span> <span class="n">5576711</span></li></ol></pre><p>On a 64-bit <code class="inline"><span class="w">perl</span></code>
, you can get even larger dates for those really long
running projects:</p>
<pre class="verbatim"><ol><li>    <span class="i">% perl5</span>.<span class="n">12</span> -<span class="w">E</span> <span class="q">&#39;say scalar gmtime( 0x9FFF_FFFFFFFF )&#39;</span></li><li>    <span class="w">Thu</span> <span class="w">Nov</span>  <span class="n">2</span> <span class="n">00</span><span class="co">:</span><span class="n">42</span><span class="co">:</span><span class="n">39</span> <span class="n">5576711</span></li></ol></pre><p>You're still out of luck if you need to keep track of decaying protons
though.</p>
<a name="Data%3a-Strings"></a><h1>Data: Strings</h1>
<a name="How-do-I-validate-input%3f"></a><h2>How do I validate input?</h2>
<p>(contributed by brian d foy)</p>
<p>There are many ways to ensure that values are what you expect or
want to accept. Besides the specific examples that we cover in the
perlfaq, you can also look at the modules with "Assert" and "Validate"
in their names, along with other modules such as <a href="http://search.cpan.org/perldoc/Regexp::Common">Regexp::Common</a>.</p>
<p>Some modules have validation for particular types of input, such
as <a href="http://search.cpan.org/perldoc/Business::ISBN">Business::ISBN</a>, <a href="http://search.cpan.org/perldoc/Business::CreditCard">Business::CreditCard</a>, <a href="http://search.cpan.org/perldoc/Email::Valid">Email::Valid</a>,
and <a href="http://search.cpan.org/perldoc/Data::Validate::IP">Data::Validate::IP</a>.</p>
<a name="How-do-I-unescape-a-string%3f"></a><h2>How do I unescape a string?</h2>
<p>It depends just what you mean by "escape". URL escapes are dealt
with in <a href="perlfaq9.html">perlfaq9</a>. Shell escapes with the backslash (<code class="inline">\</code>
)
character are removed with</p>
<pre class="verbatim"><ol><li>    <span class="q">s/\\(.)/$1/g</span><span class="sc">;</span></li></ol></pre><p>This won't expand <code class="inline"><span class="q">&quot;\n&quot;</span></code>
 or <code class="inline"><span class="q">&quot;\t&quot;</span></code>
 or any other special escapes.</p>
<a name="How-do-I-remove-consecutive-pairs-of-characters%3f"></a><h2>How do I remove consecutive pairs of characters?</h2>
<p>(contributed by brian d foy)</p>
<p>You can use the substitution operator to find pairs of characters (or
runs of characters) and replace them with a single instance. In this
substitution, we find a character in <code class="inline">(.)</code>. The memory parentheses
store the matched character in the back-reference <code class="inline">\<span class="w">g1</span></code>
 and we use
that to require that the same thing immediately follow it. We replace
that part of the string with the character in <code class="inline"><span class="i">$1</span></code>
.</p>
<pre class="verbatim"><ol><li>    <span class="q">s/(.)\g1/$1/g</span><span class="sc">;</span></li></ol></pre><p>We can also use the transliteration operator, <code class="inline"><a class="l_k" href="functions/tr.html">tr///</a></code>. In this
example, the search list side of our <code class="inline"><a class="l_k" href="functions/tr.html">tr///</a></code> contains nothing, but
the <code class="inline"><span class="w">c</span></code>
 option complements that so it contains everything. The
replacement list also contains nothing, so the transliteration is
almost a no-op since it won't do any replacements (or more exactly,
replace the character with itself). However, the <code class="inline"><a class="l_k" href="functions/s.html">s</a></code> option squashes
duplicated and consecutive characters in the string so a character
does not show up next to itself</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <span class="q">&#39;Haarlem&#39;</span><span class="sc">;</span>   <span class="c"># in the Netherlands</span></li><li>    <span class="i">$str</span> =~ <span class="q">tr///cs</span><span class="sc">;</span>       <span class="c"># Now Harlem, like in New York</span></li></ol></pre><a name="How-do-I-expand-function-calls-in-a-string%3f"></a><h2>How do I expand function calls in a string?</h2>
<p>(contributed by brian d foy)</p>
<p>This is documented in <a href="perlref.html">perlref</a>, and although it's not the easiest
thing to read, it does work. In each of these examples, we call the
function inside the braces used to dereference a reference. If we
have more than one return value, we can construct and dereference an
anonymous array. In this case, we call the function in list context.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The time values are @{ [localtime] }.\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If we want to call the function in scalar context, we have to do a bit
more work. We can really have any code we like inside the braces, so
we simply have to end with the scalar reference, although how you do
that is up to you, and you can use code inside the braces. Note that
the use of parens creates a list context, so we need <code class="inline"><a class="l_k" href="functions/scalar.html">scalar</a></code> to
force the scalar context on the function:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The time is ${\(scalar localtime)}.\n&quot;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The time is ${ my $x = localtime; \$x }.\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If your function already returns a reference, you don't need to create
the reference yourself.</p>
<pre class="verbatim"><ol><li><a name="timestamp"></a>    sub <span class="m">timestamp</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$t</span> = <a class="l_k" href="functions/localtime.html">localtime</a><span class="sc">;</span> \<span class="i">$t</span> <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The time is ${ timestamp() }.\n&quot;</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">Interpolation</span></code>
 module can also do a lot of magic for you. You can
specify a variable name, in this case <code class="inline"><span class="w">E</span></code>
, to set up a tied hash that
does the interpolation for you. It has several other methods to do this
as well.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Interpolation</span> <span class="w">E</span> <span class="cm">=&gt;</span> <span class="q">&#39;eval&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The time values are $E{localtime()}.\n&quot;</span><span class="sc">;</span></li></ol></pre><p>In most cases, it is probably easier to simply use string concatenation,
which also forces scalar context.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The time is &quot;</span> . <a class="l_k" href="functions/localtime.html">localtime</a><span class="s">(</span><span class="s">)</span> . <span class="q">&quot;.\n&quot;</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-find-matching%2fnesting-anything%3f"></a><h2>How do I find matching/nesting anything?</h2>
<p>To find something between two single
characters, a pattern like <code class="inline"><span class="q">/x([^x]*)x/</span></code>
 will get the intervening
bits in $1. For multiple ones, then something more like
<code class="inline"><span class="q">/alpha(.*?)omega/</span></code>
 would be needed. For nested patterns
and/or balanced expressions, see the so-called
<a href="perlre.html#(%3fPARNO)-(%3f-PARNO)-(%3f%2bPARNO)-(%3fR)-(%3f0)">(?PARNO)</a>
construct (available since perl 5.10).
The CPAN module <a href="http://search.cpan.org/perldoc/Regexp::Common">Regexp::Common</a> can help to build such
regular expressions (see in particular
<a href="http://search.cpan.org/perldoc/Regexp::Common::balanced">Regexp::Common::balanced</a> and <a href="http://search.cpan.org/perldoc/Regexp::Common::delimited">Regexp::Common::delimited</a>).</p>
<p>More complex cases will require to write a parser, probably
using a parsing module from CPAN, like
<a href="http://search.cpan.org/perldoc/Regexp::Grammars">Regexp::Grammars</a>, <a href="http://search.cpan.org/perldoc/Parse::RecDescent">Parse::RecDescent</a>, <a href="http://search.cpan.org/perldoc/Parse::Yapp">Parse::Yapp</a>,
<a href="Text/Balanced.html">Text::Balanced</a>, or <a href="http://search.cpan.org/perldoc/Marpa::R2">Marpa::R2</a>.</p>
<a name="How-do-I-reverse-a-string%3f"></a><h2>How do I reverse a string?</h2>
<p>Use <code class="inline"><a class="l_k" href="functions/reverse.html">reverse()</a></code> in scalar context, as documented in
<a href="functions/reverse.html">reverse</a>.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$reversed</span> = <a class="l_k" href="functions/reverse.html">reverse</a> <span class="i">$string</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-expand-tabs-in-a-string%3f"></a><h2>How do I expand tabs in a string?</h2>
<p>You can do it yourself:</p>
<pre class="verbatim"><ol><li>    <span class="n">1</span> while <span class="i">$string</span> =~ <span class="q">s/\t+/&#39; &#39; x (length($&amp;) * 8 - length($`) % 8)/e</span><span class="sc">;</span></li></ol></pre><p>Or you can just use the <a href="Text/Tabs.html">Text::Tabs</a> module (part of the standard Perl
distribution).</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Text::Tabs</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@expanded_lines</span> = <span class="i">expand</span><span class="s">(</span><span class="i">@lines_with_tabs</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-reformat-a-paragraph%3f"></a><h2>How do I reformat a paragraph?</h2>
<p>Use <a href="Text/Wrap.html">Text::Wrap</a> (part of the standard Perl distribution):</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Text::Wrap</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">wrap</span><span class="s">(</span><span class="q">&quot;\t&quot;</span><span class="cm">,</span> <span class="q">&#39;  &#39;</span><span class="cm">,</span> <span class="i">@paragraphs</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The paragraphs you give to <a href="Text/Wrap.html">Text::Wrap</a> should not contain embedded
newlines. <a href="Text/Wrap.html">Text::Wrap</a> doesn't justify the lines (flush-right).</p>
<p>Or use the CPAN module <a href="http://search.cpan.org/perldoc/Text::Autoformat">Text::Autoformat</a>. Formatting files can be
easily done by making a shell alias, like so:</p>
<pre class="verbatim"><ol><li>    <span class="w">alias</span> <span class="w">fmt</span>=<span class="q">&quot;perl -i -MText::Autoformat -n0777 \</span></li><li>        <span class="q">        -e &#39;print autoformat $_, {all=&gt;1}&#39; $*&quot;</span></li></ol></pre><p>See the documentation for <a href="http://search.cpan.org/perldoc/Text::Autoformat">Text::Autoformat</a> to appreciate its many
capabilities.</p>
<a name="How-can-I-access-or-change-N-characters-of-a-string%3f"></a><h2>How can I access or change N characters of a string?</h2>
<p>You can access the first characters of a string with substr().
To get the first character, for example, start at position 0
and grab the string of length 1.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <span class="q">&quot;Just another Perl Hacker&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$first_char</span> = <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span> <span class="i">$string</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">1</span> <span class="s">)</span><span class="sc">;</span>  <span class="c">#  &#39;J&#39;</span></li></ol></pre><p>To change part of a string, you can use the optional fourth
argument which is the replacement string.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span> <span class="i">$string</span><span class="cm">,</span> <span class="n">13</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="q">&quot;Perl 5.8.0&quot;</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can also use substr() as an lvalue.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span> <span class="i">$string</span><span class="cm">,</span> <span class="n">13</span><span class="cm">,</span> <span class="n">4</span> <span class="s">)</span> =  <span class="q">&quot;Perl 5.8.0&quot;</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-change-the-Nth-occurrence-of-something%3f"></a><h2>How do I change the Nth occurrence of something?</h2>
<p>You have to keep track of N yourself. For example, let's say you want
to change the fifth occurrence of <code class="inline"><span class="q">&quot;whoever&quot;</span></code>
 or <code class="inline"><span class="q">&quot;whomever&quot;</span></code>
 into
<code class="inline"><span class="q">&quot;whosoever&quot;</span></code>
 or <code class="inline"><span class="q">&quot;whomsoever&quot;</span></code>
, case insensitively. These
all assume that $_ contains the string to be altered.</p>
<pre class="verbatim"><ol><li>    <span class="i">$count</span> = <span class="n">0</span><span class="sc">;</span></li><li>    <span class="q">s{((whom?)ever)}{</span></li><li>    <span class="q">    ++$count == 5       # is it the 5th?</span></li><li>        <span class="q">        ? &quot;${2}soever&quot;  # yes, swap</span></li><li>        <span class="q">        : $1            # renege and leave it there</span></li><li>        <span class="q">        }ige</span><span class="sc">;</span></li></ol></pre><p>In the more general case, you can use the <code class="inline">/g</code> modifier in a <code class="inline">while</code>

loop, keeping count of matches.</p>
<pre class="verbatim"><ol><li>    <span class="i">$WANT</span> = <span class="n">3</span><span class="sc">;</span></li><li>    <span class="i">$count</span> = <span class="n">0</span><span class="sc">;</span></li><li>    <span class="i">$_</span> = <span class="q">&quot;One fish two fish red fish blue fish&quot;</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="q">/(\w+)\s+fish\b/gi</span><span class="s">)</span> <span class="s">{</span></li><li>        if <span class="s">(</span>++<span class="i">$count</span> == <span class="i">$WANT</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The third fish is a $1 one.\n&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>That prints out: <code class="inline"><span class="q">&quot;The third fish is a red one.&quot;</span></code>
  You can also use a
repetition count and repeated pattern like this:</p>
<pre class="verbatim"><ol><li>    <span class="q">/(?:\w+\s+fish\s+){2}(\w+)\s+fish/i</span><span class="sc">;</span></li></ol></pre><a name="How-can-I-count-the-number-of-occurrences-of-a-substring-within-a-string%3f"></a><h2>How can I count the number of occurrences of a substring within a string?</h2>
<p>There are a number of ways, with varying efficiency. If you want a
count of a certain single character (X) within a string, you can use the
<code class="inline"><a class="l_k" href="functions/tr.html">tr///</a></code> function like so:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <span class="q">&quot;ThisXlineXhasXsomeXx&#39;sXinXit&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="s">(</span><span class="i">$string</span> =~ <span class="q">tr/X//</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;There are $count X characters in the string&quot;</span><span class="sc">;</span></li></ol></pre><p>This is fine if you are just looking for a single character. However,
if you are trying to count multiple character substrings within a
larger string, <code class="inline"><a class="l_k" href="functions/tr.html">tr///</a></code> won't work. What you can do is wrap a while()
loop around a global pattern match. For example, let's count negative
integers:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <span class="q">&quot;-9 55 48 -2 23 -76 4 14 -44&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="n">0</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="i">$string</span> =~ <span class="q">/-\d+/g</span><span class="s">)</span> <span class="s">{</span> <span class="i">$count</span>++ <span class="s">}</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;There are $count negative numbers in the string&quot;</span><span class="sc">;</span></li></ol></pre><p>Another version uses a global match in list context, then assigns the
result to a scalar, producing a count of the number of matches.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="s">(</span><span class="s">)</span> = <span class="i">$string</span> =~ <span class="q">/-\d+/g</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-capitalize-all-the-words-on-one-line%3f"></a><h2>How do I capitalize all the words on one line?
   </h2>
<p>(contributed by brian d foy)</p>
<p>Damian Conway's <a href="http://search.cpan.org/perldoc/Text::Autoformat">Text::Autoformat</a> handles all of the thinking
for you.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Text::Autoformat</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$x</span> = <span class="q">&quot;Dr. Strangelove or: How I Learned to Stop &quot;</span>.</li><li>      <span class="q">&quot;Worrying and Love the Bomb&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$style</span> <span class="s">(</span><span class="q">qw( sentence title highlight )</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">autoformat</span><span class="s">(</span><span class="i">$x</span><span class="cm">,</span> <span class="s">{</span> <span class="w">case</span> <span class="cm">=&gt;</span> <span class="i">$style</span> <span class="s">}</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>How do you want to capitalize those words?</p>
<pre class="verbatim"><ol><li>    <span class="w">FRED</span> <span class="w">AND</span> <span class="w">BARNEY&#39;S</span> <span class="w">LODGE</span>        <span class="c"># all uppercase</span></li><li>    <span class="w">Fred</span> <span class="w">And</span> <span class="w">Barney&#39;s</span> <span class="w">Lodge</span>        <span class="c"># title case</span></li><li>    <span class="w">Fred</span> <a class="l_k" href="functions/and.html">and</a> <span class="w">Barney&#39;s</span> <span class="w">Lodge</span>        <span class="c"># highlight case</span></li></ol></pre><p>It's not as easy a problem as it looks. How many words do you think
are in there? Wait for it... wait for it.... If you answered 5
you're right. Perl words are groups of <code class="inline">\<span class="w">w</span>+</code>
, but that's not what
you want to capitalize. How is Perl supposed to know not to capitalize
that <code class="inline"><a class="l_k" href="functions/s.html">s</a></code> after the apostrophe? You could try a regular expression:</p>
<pre class="verbatim"><ol><li>    <span class="i">$string</span> =~ <span class="q">s/ (</span></li><li>                 <span class="q">                 (^\w)    #at the beginning of the line</span></li><li>                   <span class="q">                   |      # or</span></li><li>                 <span class="q">                 (\s\w)   #preceded by whitespace</span></li><li>                   <span class="q">                   )</span></li><li>                <span class="q">                /\U$1/xg</span><span class="sc">;</span></li><li></li><li>    <span class="i">$string</span> =~ <span class="q">s/([\w&#39;]+)/\u\L$1/g</span><span class="sc">;</span></li></ol></pre><p>Now, what if you don't want to capitalize that "and"? Just use
<a href="http://search.cpan.org/perldoc/Text::Autoformat">Text::Autoformat</a> and get on with the next problem. :)</p>
<a name="How-can-I-split-a-%5bcharacter%5d-delimited-string-except-when-inside-%5bcharacter%5d%3f"></a><h2>How can I split a [character]-delimited string except when inside [character]?</h2>
<p>Several modules can handle this sort of parsing--<a href="Text/Balanced.html">Text::Balanced</a>,
<a href="http://search.cpan.org/perldoc/Text::CSV">Text::CSV</a>, <a href="http://search.cpan.org/perldoc/Text::CSV_XS">Text::CSV_XS</a>, and <a href="Text/ParseWords.html">Text::ParseWords</a>, among others.</p>
<p>Take the example case of trying to split a string that is
comma-separated into its different fields. You can't use <code class="inline"><a class="l_k" href="functions/split.html">split(/,/)</a></code>
because you shouldn't split if the comma is inside quotes. For
example, take a data line like this:</p>
<pre class="verbatim"><ol><li>    <span class="w">SAR001</span><span class="cm">,</span><span class="q">&quot;&quot;</span><span class="cm">,</span><span class="q">&quot;Cimetrix, Inc&quot;</span><span class="cm">,</span><span class="q">&quot;Bob Smith&quot;</span><span class="cm">,</span><span class="q">&quot;CAM&quot;</span><span class="cm">,</span><span class="w">N</span><span class="cm">,</span><span class="n">8</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><span class="n">7</span><span class="cm">,</span><span class="q">&quot;Error, Core Dumped&quot;</span></li></ol></pre><p>Due to the restriction of the quotes, this is a fairly complex
problem. Thankfully, we have Jeffrey Friedl, author of
<i>Mastering Regular Expressions</i>, to handle these for us. He
suggests (assuming your string is contained in <code class="inline"><span class="i">$text</span></code>
):</p>
<pre class="verbatim"><ol><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">@new</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@new</span><span class="cm">,</span> <span class="i">$+</span><span class="s">)</span> <a class="l_k" href="functions/while.html">while</a> <span class="i">$text</span> =~ <span class="q">m{</span></li><li>         <span class="q">         &quot;([^\&quot;\\]*(?:\\.[^\&quot;\\]*)*)&quot;,? # groups the phrase inside the quotes</span></li><li>        <span class="q">        | ([^,]+),?</span></li><li>        <span class="q">        | ,</span></li><li>     <span class="q">     }gx</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@new</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span><span class="i">$text</span><span class="cm">,</span><span class="n">-1</span><span class="cm">,</span><span class="n">1</span><span class="s">)</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;,&#39;</span><span class="sc">;</span></li></ol></pre><p>If you want to represent quotation marks inside a
quotation-mark-delimited field, escape them with backslashes (eg,
<code class="inline"><span class="q">&quot;like \&quot;this\&quot;&quot;</span></code>
.</p>
<p>Alternatively, the <a href="Text/ParseWords.html">Text::ParseWords</a> module (part of the standard
Perl distribution) lets you say:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Text::ParseWords</span><span class="sc">;</span></li><li>    <span class="i">@new</span> = <span class="i">quotewords</span><span class="s">(</span><span class="q">&quot;,&quot;</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="i">$text</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>For parsing or generating CSV, though, using <a href="http://search.cpan.org/perldoc/Text::CSV">Text::CSV</a> rather than
implementing it yourself is highly recommended; you'll save yourself odd bugs
popping up later by just using code which has already been tried and tested in
production for years.</p>
<a name="How-do-I-strip-blank-space-from-the-beginning%2fend-of-a-string%3f"></a><h2>How do I strip blank space from the beginning/end of a string?</h2>
<p>(contributed by brian d foy)</p>
<p>A substitution can do this for you. For a single line, you want to
replace all the leading or trailing whitespace with nothing. You
can do that with a pair of substitutions:</p>
<pre class="verbatim"><ol><li>    <span class="q">s/^\s+//</span><span class="sc">;</span></li><li>    <span class="q">s/\s+$//</span><span class="sc">;</span></li></ol></pre><p>You can also write that as a single substitution, although it turns
out the combined statement is slower than the separate ones. That
might not matter to you, though:</p>
<pre class="verbatim"><ol><li>    <span class="q">s/^\s+|\s+$//g</span><span class="sc">;</span></li></ol></pre><p>In this regular expression, the alternation matches either at the
beginning or the end of the string since the anchors have a lower
precedence than the alternation. With the <code class="inline">/g</code> flag, the substitution
makes all possible matches, so it gets both. Remember, the trailing
newline matches the <code class="inline">\s+</code>, and  the <code class="inline"><span class="i">$</span></code>
 anchor can match to the
absolute end of the string, so the newline disappears too. Just add
the newline to the output, which has the added benefit of preserving
"blank" (consisting entirely of whitespace) lines which the <code class="inline">^\s+</code>
would remove all by itself:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a><span class="s">(</span> &lt;&gt; <span class="s">)</span> <span class="s">{</span></li><li>        <span class="q">s/^\s+|\s+$//g</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$_\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>For a multi-line string, you can apply the regular expression to each
logical line in the string by adding the <code class="inline">/m</code> flag (for
"multi-line"). With the <code class="inline">/m</code> flag, the <code class="inline"><span class="i">$</span></code>
 matches <i>before</i> an
embedded newline, so it doesn't remove it. This pattern still removes
the newline at the end of the string:</p>
<pre class="verbatim"><ol><li>    <span class="i">$string</span> =~ <span class="q">s/^\s+|\s+$//gm</span><span class="sc">;</span></li></ol></pre><p>Remember that lines consisting entirely of whitespace will disappear,
since the first part of the alternation can match the entire string
and replace it with nothing. If you need to keep embedded blank lines,
you have to do a little more work. Instead of matching any whitespace
(since that includes a newline), just match the other whitespace:</p>
<pre class="verbatim"><ol><li>    <span class="i">$string</span> =~ <span class="q">s/^[\t\f ]+|[\t\f ]+$//mg</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-pad-a-string-with-blanks-or-pad-a-number-with-zeroes%3f"></a><h2>How do I pad a string with blanks or pad a number with zeroes?</h2>
<p>In the following examples, <code class="inline"><span class="i">$pad_len</span></code>
 is the length to which you wish
to pad the string, <code class="inline"><span class="i">$text</span></code>
 or <code class="inline"><span class="i">$num</span></code>
 contains the string to be padded,
and <code class="inline"><span class="i">$pad_char</span></code>
 contains the padding character. You can use a single
character string constant instead of the <code class="inline"><span class="i">$pad_char</span></code>
 variable if you
know what it is in advance. And in the same way you can use an integer in
place of <code class="inline"><span class="i">$pad_len</span></code>
 if you know the pad length in advance.</p>
<p>The simplest method uses the <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code> function. It can pad on the left
or right with blanks and on the left with zeroes and it will not
truncate the result. The <code class="inline"><a class="l_k" href="functions/pack.html">pack</a></code> function can only pad strings on the
right with blanks and it will truncate the result to a maximum length of
<code class="inline"><span class="i">$pad_len</span></code>
.</p>
<pre class="verbatim"><ol><li>    <span class="c"># Left padding a string with blanks (no truncation):</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%${pad_len}s&quot;</span><span class="cm">,</span> <span class="i">$text</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%*s&quot;</span><span class="cm">,</span> <span class="i">$pad_len</span><span class="cm">,</span> <span class="i">$text</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># same thing</span></li><li></li><li>    <span class="c"># Right padding a string with blanks (no truncation):</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%-${pad_len}s&quot;</span><span class="cm">,</span> <span class="i">$text</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%-*s&quot;</span><span class="cm">,</span> <span class="i">$pad_len</span><span class="cm">,</span> <span class="i">$text</span><span class="s">)</span><span class="sc">;</span> <span class="c"># same thing</span></li><li></li><li>    <span class="c"># Left padding a number with 0 (no truncation):</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%0${pad_len}d&quot;</span><span class="cm">,</span> <span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%0*d&quot;</span><span class="cm">,</span> <span class="i">$pad_len</span><span class="cm">,</span> <span class="i">$num</span><span class="s">)</span><span class="sc">;</span> <span class="c"># same thing</span></li><li></li><li>    <span class="c"># Right padding a string with blanks using pack (will truncate):</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;A$pad_len&quot;</span><span class="cm">,</span><span class="i">$text</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you need to pad with a character other than blank or zero you can use
one of the following methods. They all generate a pad string with the
<code class="inline"><span class="w">x</span></code>
 operator and combine that with <code class="inline"><span class="i">$text</span></code>
. These methods do
not truncate <code class="inline"><span class="i">$text</span></code>
.</p>
<p>Left and right padding with any character, creating a new string:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <span class="i">$pad_char</span> x <span class="s">(</span> <span class="i">$pad_len</span> - <a class="l_k" href="functions/length.html">length</a><span class="s">(</span> <span class="i">$text</span> <span class="s">)</span> <span class="s">)</span> . <span class="i">$text</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$padded</span> = <span class="i">$text</span> . <span class="i">$pad_char</span> x <span class="s">(</span> <span class="i">$pad_len</span> - <a class="l_k" href="functions/length.html">length</a><span class="s">(</span> <span class="i">$text</span> <span class="s">)</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Left and right padding with any character, modifying <code class="inline"><span class="i">$text</span></code>
 directly:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span> <span class="i">$text</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span> <span class="s">)</span> = <span class="i">$pad_char</span> x <span class="s">(</span> <span class="i">$pad_len</span> - <a class="l_k" href="functions/length.html">length</a><span class="s">(</span> <span class="i">$text</span> <span class="s">)</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$text</span> .= <span class="i">$pad_char</span> x <span class="s">(</span> <span class="i">$pad_len</span> - <a class="l_k" href="functions/length.html">length</a><span class="s">(</span> <span class="i">$text</span> <span class="s">)</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-extract-selected-columns-from-a-string%3f"></a><h2>How do I extract selected columns from a string?</h2>
<p>(contributed by brian d foy)</p>
<p>If you know the columns that contain the data, you can
use <code class="inline"><a class="l_k" href="functions/substr.html">substr</a></code> to extract a single column.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$column</span> = <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span> <span class="i">$line</span><span class="cm">,</span> <span class="i">$start_column</span><span class="cm">,</span> <span class="i">$length</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can use <code class="inline"><a class="l_k" href="functions/split.html">split</a></code> if the columns are separated by whitespace or
some other delimiter, as long as whitespace or the delimiter cannot
appear as part of the data.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span>    = <span class="q">&#39; fred barney   betty   &#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@columns</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">/\s+/</span><span class="cm">,</span> <span class="i">$line</span><span class="sc">;</span></li><li>        <span class="c"># ( &#39;&#39;, &#39;fred&#39;, &#39;barney&#39;, &#39;betty&#39; );</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span>    = <span class="q">&#39;fred||barney||betty&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@columns</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">/\|/</span><span class="cm">,</span> <span class="i">$line</span><span class="sc">;</span></li><li>        <span class="c"># ( &#39;fred&#39;, &#39;&#39;, &#39;barney&#39;, &#39;&#39;, &#39;betty&#39; );</span></li></ol></pre><p>If you want to work with comma-separated values, don't do this since
that format is a bit more complicated. Use one of the modules that
handle that format, such as <a href="http://search.cpan.org/perldoc/Text::CSV">Text::CSV</a>, <a href="http://search.cpan.org/perldoc/Text::CSV_XS">Text::CSV_XS</a>, or
<a href="http://search.cpan.org/perldoc/Text::CSV_PP">Text::CSV_PP</a>.</p>
<p>If you want to break apart an entire line of fixed columns, you can use
<code class="inline"><a class="l_k" href="functions/unpack.html">unpack</a></code> with the A (ASCII) format. By using a number after the format
specifier, you can denote the column width. See the <code class="inline"><a class="l_k" href="functions/pack.html">pack</a></code> and <code class="inline"><a class="l_k" href="functions/unpack.html">unpack</a></code>
entries in <a href="perlfunc.html">perlfunc</a> for more details.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@fields</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span> <span class="i">$line</span><span class="cm">,</span> <span class="q">&quot;A8 A8 A8 A16 A4&quot;</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note that spaces in the format argument to <code class="inline"><a class="l_k" href="functions/unpack.html">unpack</a></code> do not denote literal
spaces. If you have space separated data, you may want <code class="inline"><a class="l_k" href="functions/split.html">split</a></code> instead.</p>
<a name="How-do-I-find-the-soundex-value-of-a-string%3f"></a><h2>How do I find the soundex value of a string?</h2>
<p>(contributed by brian d foy)</p>
<p>You can use the <code class="inline"><span class="w">Text::Soundex</span></code>
 module. If you want to do fuzzy or close
matching, you might also try the <a href="http://search.cpan.org/perldoc/String::Approx">String::Approx</a>, and
<a href="http://search.cpan.org/perldoc/Text::Metaphone">Text::Metaphone</a>, and <a href="http://search.cpan.org/perldoc/Text::DoubleMetaphone">Text::DoubleMetaphone</a> modules.</p>
<a name="How-can-I-expand-variables-in-text-strings%3f"></a><h2>How can I expand variables in text strings?</h2>
<p>(contributed by brian d foy)</p>
<p>If you can avoid it, don't, or if you can use a templating system,
such as <a href="http://search.cpan.org/perldoc/Text::Template">Text::Template</a> or <a href="http://search.cpan.org/perldoc/Template">Template</a> Toolkit, do that instead. You
might even be able to get the job done with <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code> or <code class="inline"><a class="l_k" href="functions/printf.html">printf</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a> <span class="q">&#39;Say hello to %s and %s&#39;</span><span class="cm">,</span> <span class="i">$foo</span><span class="cm">,</span> <span class="i">$bar</span><span class="sc">;</span></li></ol></pre><p>However, for the one-off simple case where I don't want to pull out a
full templating system, I'll use a string that has two Perl scalar
variables in it. In this example, I want to expand <code class="inline"><span class="i">$foo</span></code>
 and <code class="inline"><span class="i">$bar</span></code>

to their variable's values:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$foo</span> = <span class="q">&#39;Fred&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$bar</span> = <span class="q">&#39;Barney&#39;</span><span class="sc">;</span></li><li>    <span class="i">$string</span> = <span class="q">&#39;Say hello to $foo and $bar&#39;</span><span class="sc">;</span></li></ol></pre><p>One way I can do this involves the substitution operator and a double
<code class="inline">/e</code> flag. The first <code class="inline">/e</code> evaluates <code class="inline"><span class="i">$1</span></code>
 on the replacement side and
turns it into <code class="inline"><span class="i">$foo</span></code>
. The second /e starts with <code class="inline"><span class="i">$foo</span></code>
 and replaces
it with its value. <code class="inline"><span class="i">$foo</span></code>
, then, turns into 'Fred', and that's finally
what's left in the string:</p>
<pre class="verbatim"><ol><li>    <span class="i">$string</span> =~ <span class="q">s/(\$\w+)/$1/eeg</span><span class="sc">;</span> <span class="c"># &#39;Say hello to Fred and Barney&#39;</span></li></ol></pre><p>The <code class="inline">/e</code> will also silently ignore violations of strict, replacing
undefined variable names with the empty string. Since I'm using the
<code class="inline">/e</code> flag (twice even!), I have all of the same security problems I
have with <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> in its string form. If there's something odd in
<code class="inline"><span class="i">$foo</span></code>
, perhaps something like <code class="inline"><span class="i">@</span>{<span class="s">[</span> <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;rm -rf /&quot;</span> <span class="s">]</span>}</code>
, then
I could get myself in trouble.</p>
<p>To get around the security problem, I could also pull the values from
a hash instead of evaluating variable names. Using a single <code class="inline">/e</code>, I
can check the hash to ensure the value exists, and if it doesn't, I
can replace the missing value with a marker, in this case <code class="inline">???</code> to
signal that I missed something:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <span class="q">&#39;This has $foo and $bar&#39;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%Replacements</span> = <span class="s">(</span></li><li>        <span class="w">foo</span>  <span class="cm">=&gt;</span> <span class="q">&#39;Fred&#39;</span><span class="cm">,</span></li><li>        <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># $string =~ s/\$(\w+)/$Replacements{$1}/g;</span></li><li>    <span class="i">$string</span> =~ <span class="q">s/\$(\w+)/</span></li><li>        <span class="q">        exists $Replacements{$1} ? $Replacements{$1} : &#39;???&#39;</span></li><li>        <span class="q">        /eg</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$string</span><span class="sc">;</span></li></ol></pre><a name="What's-wrong-with-always-quoting-%22%24vars%22%3f"></a><h2>What's wrong with always quoting "$vars"?</h2>
<p>The problem is that those double-quotes force
stringification--coercing numbers and references into strings--even
when you don't want them to be strings. Think of it this way:
double-quote expansion is used to produce new strings. If you already
have a string, why do you need more?</p>
<p>If you get used to writing odd things like these:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$var&quot;</span><span class="sc">;</span>       <span class="c"># BAD</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$new</span> = <span class="q">&quot;$old&quot;</span><span class="sc">;</span>       <span class="c"># BAD</span></li><li>    <span class="i">somefunc</span><span class="s">(</span><span class="q">&quot;$var&quot;</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># BAD</span></li></ol></pre><p>You'll be in trouble. Those should (in 99.8% of the cases) be
the simpler and more direct:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$var</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$new</span> = <span class="i">$old</span><span class="sc">;</span></li><li>    <span class="i">somefunc</span><span class="s">(</span><span class="i">$var</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Otherwise, besides slowing you down, you're going to break code when
the thing in the scalar is actually neither a string nor a number, but
a reference:</p>
<pre class="verbatim"><ol><li>    <span class="i">func</span><span class="s">(</span>\<span class="i">@array</span><span class="s">)</span><span class="sc">;</span></li><li><a name="func"></a>    sub <span class="m">func</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$aref</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$oref</span> = <span class="q">&quot;$aref&quot;</span><span class="sc">;</span>  <span class="c"># WRONG</span></li><li>    <span class="s">}</span></li></ol></pre><p>You can also get into subtle problems on those few operations in Perl
that actually do care about the difference between a string and a
number, such as the magical <code class="inline">++</code>
 autoincrement operator or the
syscall() function.</p>
<p>Stringification also destroys arrays.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@lines</span> = <span class="q">`command`</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;@lines&quot;</span><span class="sc">;</span>     <span class="c"># WRONG - extra blanks</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">@lines</span><span class="sc">;</span>       <span class="c"># right</span></li></ol></pre><a name="Why-don't-my-%3c%3cHERE-documents-work%3f"></a><h2>Why don't my &lt;&lt;HERE documents work?</h2>
<p>Here documents are found in <a href="perlop.html">perlop</a>. Check for these three things:</p>
<ul>
<li><a name="There-must-be-no-space-after-the-%3c%3c-part."></a><b>There must be no space after the &lt;&lt; part.</b>
</li>
<li><a name="There-(probably)-should-be-a-semicolon-at-the-end-of-the-opening-token"></a><b>There (probably) should be a semicolon at the end of the opening token</b>
</li>
<li><a name="You-can't-(easily)-have-any-space-in-front-of-the-tag."></a><b>You can't (easily) have any space in front of the tag.</b>
</li>
<li><a name="There-needs-to-be-at-least-a-line-separator-after-the-end-token."></a><b>There needs to be at least a line separator after the end token.</b>
</li>
</ul>
<p>If you want to indent the text in the here document, you
can do this:</p>
<pre class="verbatim"><ol><li>    <span class="c"># all in one</span></li><li>    <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$VAR</span> = <span class="h">&lt;&lt;HERE_TARGET</span><span class="s">)</span> =~ <span class="q">s/^\s+//gm</span><span class="sc">;</span></li><li><span class="hh">        your text</span></li><li><span class="hh">        goes here</span></li><li><span class="hh">    HERE_TARGET</span></li></ol></pre><p>But the HERE_TARGET must still be flush against the margin.
If you want that indented also, you'll have to quote
in the indentation.</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$quote</span> = <span class="h">&lt;&lt;&#39;    FINIS&#39;</span><span class="s">)</span> =~ <span class="q">s/^\s+//gm</span><span class="sc">;</span></li><li><span class="hh">            ...we will have peace, when you and all your works have</span></li><li><span class="hh">            perished--and the works of your dark master to whom you</span></li><li><span class="hh">            would deliver us. You are a liar, Saruman, and a corrupter</span></li><li><span class="hh">            of men&#39;s hearts. --Theoden in /usr/src/perl/taint.c</span></li><li><span class="hh">        FINIS</span></li><li><span class="hh">    $quote =~ s/\s+--/\n--/;</span></li></ol></pre><p>A nice general-purpose fixer-upper function for indented here documents
follows. It expects to be called with a here document as its argument.
It looks to see whether each line begins with a common substring, and
if so, strips that substring off. Otherwise, it takes the amount of leading
whitespace found on the first line and removes that much off each
subsequent line.</p>
<pre class="verbatim"><ol><li><a name="fix"></a>    sub <span class="m">fix</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/local.html">local</a> <span class="i">$_</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$white</span><span class="cm">,</span> <span class="i">$leader</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># common whitespace and common leading string</span></li><li>        if <span class="s">(</span><span class="q">/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\g1\g2?.*\n)+$/</span><span class="s">)</span> <span class="s">{</span></li><li>            <span class="s">(</span><span class="i">$white</span><span class="cm">,</span> <span class="i">$leader</span><span class="s">)</span> = <span class="s">(</span><span class="i">$2</span><span class="cm">,</span> <a class="l_k" href="functions/quotemeta.html">quotemeta</a><span class="s">(</span><span class="i">$1</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span> else <span class="s">{</span></li><li>            <span class="s">(</span><span class="i">$white</span><span class="cm">,</span> <span class="i">$leader</span><span class="s">)</span> = <span class="s">(</span><span class="q">/^(\s+)/</span><span class="cm">,</span> <span class="q">&#39;&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <span class="q">s/^\s*?$leader(?:$white)?//gm</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="i">$_</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>This works with leading special strings, dynamically determined:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$remember_the_main</span> = <span class="w">fix</span><span class="h">&lt;&lt;&#39;    MAIN_INTERPRETER_LOOP&#39;</span><span class="sc">;</span></li><li><span class="hh">    @@@ int</span></li><li><span class="hh">    @@@ runops() {</span></li><li><span class="hh">    @@@     SAVEI32(runlevel);</span></li><li><span class="hh">    @@@     runlevel++;</span></li><li><span class="hh">    @@@     while ( op = (*op-&gt;op_ppaddr)() );</span></li><li><span class="hh">    @@@     TAINT_NOT;</span></li><li><span class="hh">    @@@     return 0;</span></li><li><span class="hh">    @@@ }</span></li><li><span class="h">    MAIN_INTERPRETER_LOOP</span></li></ol></pre><p>Or with a fixed amount of leading whitespace, with remaining
indentation correctly preserved:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$poem</span> = <span class="w">fix</span><span class="h">&lt;&lt;EVER_ON_AND_ON</span><span class="sc">;</span></li><li><span class="hh">       Now far ahead the Road has gone,</span></li><li><span class="hh">      And I must follow, if I can,</span></li><li><span class="hh">       Pursuing it with eager feet,</span></li><li><span class="hh">      Until it joins some larger way</span></li><li><span class="hh">       Where many paths and errands meet.</span></li><li><span class="hh">      And whither then? I cannot say.</span></li><li><span class="hh">        --Bilbo in /usr/src/perl/pp_ctl.c</span></li><li><span class="hh">    EVER_ON_AND_ON</span></li></ol></pre><a name="Data%3a-Arrays"></a><h1>Data: Arrays</h1>
<a name="What-is-the-difference-between-a-list-and-an-array%3f"></a><h2>What is the difference between a list and an array?</h2>
<p>(contributed by brian d foy)</p>
<p>A list is a fixed collection of scalars. An array is a variable that
holds a variable collection of scalars. An array can supply its collection
for list operations, so list operations also work on arrays:</p>
<pre class="verbatim"><ol><li>    <span class="c"># slices</span></li><li>    <span class="s">(</span> <span class="q">&#39;dog&#39;</span><span class="cm">,</span> <span class="q">&#39;cat&#39;</span><span class="cm">,</span> <span class="q">&#39;bird&#39;</span> <span class="s">)</span>[<span class="n">2</span><span class="cm">,</span><span class="n">3</span>]<span class="sc">;</span></li><li>    <span class="i">@animals</span>[<span class="n">2</span><span class="cm">,</span><span class="n">3</span>]<span class="sc">;</span></li><li></li><li>    <span class="c"># iteration</span></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span> <span class="q">qw( dog cat bird )</span> <span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span> <span class="i">@animals</span> <span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@three</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> <a class="l_k" href="functions/length.html">length</a> == <span class="n">3</span> <span class="s">}</span> <span class="q">qw( dog cat bird )</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@three</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> <a class="l_k" href="functions/length.html">length</a> == <span class="n">3</span> <span class="s">}</span> <span class="i">@animals</span><span class="sc">;</span></li><li></li><li>    <span class="c"># supply an argument list</span></li><li>    <span class="i">wash_animals</span><span class="s">(</span> <span class="q">qw( dog cat bird )</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">wash_animals</span><span class="s">(</span> <span class="i">@animals</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Array operations, which change the scalars, rearrange them, or add
or subtract some scalars, only work on arrays. These can't work on a
list, which is fixed. Array operations include <code class="inline"><a class="l_k" href="functions/shift.html">shift</a></code>, <code class="inline"><a class="l_k" href="functions/unshift.html">unshift</a></code>,
<code class="inline"><a class="l_k" href="functions/push.html">push</a></code>, <code class="inline"><a class="l_k" href="functions/pop.html">pop</a></code>, and <code class="inline"><a class="l_k" href="functions/splice.html">splice</a></code>.</p>
<p>An array can also change its length:</p>
<pre class="verbatim"><ol><li>    <span class="i">$#animals</span> = <span class="n">1</span><span class="sc">;</span>  <span class="c"># truncate to two elements</span></li><li>    <span class="i">$#animals</span> = <span class="n">10000</span><span class="sc">;</span> <span class="c"># pre-extend to 10,001 elements</span></li></ol></pre><p>You can change an array element, but you can't change a list element:</p>
<pre class="verbatim"><ol><li>    <span class="i">$animals</span>[<span class="n">0</span>] = <span class="q">&#39;Rottweiler&#39;</span><span class="sc">;</span></li><li>    <span class="q">qw( dog cat bird )</span><span class="s">[</span><span class="n">0</span><span class="s">]</span> = <span class="q">&#39;Rottweiler&#39;</span><span class="sc">;</span> <span class="c"># syntax error!</span></li><li></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span> <span class="i">@animals</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="q">s/^d/fr/</span><span class="sc">;</span>  <span class="c"># works fine</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span> <span class="q">qw( dog cat bird )</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="q">s/^d/fr/</span><span class="sc">;</span>  <span class="c"># Error! Modification of read only value!</span></li><li>    <span class="s">}</span></li></ol></pre><p>However, if the list element is itself a variable, it appears that you
can change a list element. However, the list element is the variable, not
the data. You're not changing the list element, but something the list
element refers to. The list element itself doesn't change: it's still
the same variable.</p>
<p>You also have to be careful about context. You can assign an array to
a scalar to get the number of elements in the array. This only works
for arrays, though:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="i">@animals</span><span class="sc">;</span>  <span class="c"># only works with arrays</span></li></ol></pre><p>If you try to do the same thing with what you think is a list, you
get a quite different result. Although it looks like you have a list
on the righthand side, Perl actually sees a bunch of scalars separated
by a comma:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$scalar</span> = <span class="s">(</span> <span class="q">&#39;dog&#39;</span><span class="cm">,</span> <span class="q">&#39;cat&#39;</span><span class="cm">,</span> <span class="q">&#39;bird&#39;</span> <span class="s">)</span><span class="sc">;</span>  <span class="c"># $scalar gets bird</span></li></ol></pre><p>Since you're assigning to a scalar, the righthand side is in scalar
context. The comma operator (yes, it's an operator!) in scalar
context evaluates its lefthand side, throws away the result, and
evaluates it's righthand side and returns the result. In effect,
that list-lookalike assigns to <code class="inline"><span class="i">$scalar</span></code>
 it's rightmost value. Many
people mess this up because they choose a list-lookalike whose
last element is also the count they expect:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$scalar</span> = <span class="s">(</span> <span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span> <span class="s">)</span><span class="sc">;</span>  <span class="c"># $scalar gets 3, accidentally</span></li></ol></pre><a name="What-is-the-difference-between-%24array%5b1%5d-and-%40array%5b1%5d%3f"></a><h2>What is the difference between $array[1] and @array[1]?</h2>
<p>(contributed by brian d foy)</p>
<p>The difference is the sigil, that special character in front of the
array name. The <code class="inline"><span class="i">$</span></code>
 sigil means "exactly one item", while the <code class="inline"><span class="i">@</span></code>

sigil means "zero or more items". The <code class="inline"><span class="i">$</span></code>
 gets you a single scalar,
while the <code class="inline"><span class="i">@</span></code>
 gets you a list.</p>
<p>The confusion arises because people incorrectly assume that the sigil
denotes the variable type.</p>
<p>The <code class="inline"><span class="i">$array</span>[<span class="n">1</span>]</code>
 is a single-element access to the array. It's going
to return the item in index 1 (or undef if there is no item there).
If you intend to get exactly one element from the array, this is the
form you should use.</p>
<p>The <code class="inline"><span class="i">@array</span>[<span class="n">1</span>]</code>
 is an array slice, although it has only one index.
You can pull out multiple elements simultaneously by specifying
additional indices as a list, like <code class="inline"><span class="i">@array</span>[<span class="n">1</span><span class="cm">,</span><span class="n">4</span><span class="cm">,</span><span class="n">3</span><span class="cm">,</span><span class="n">0</span>]</code>
.</p>
<p>Using a slice on the lefthand side of the assignment supplies list
context to the righthand side. This can lead to unexpected results.
For instance, if you want to read a single line from a filehandle,
assigning to a scalar value is fine:</p>
<pre class="verbatim"><ol><li>    <span class="i">$array</span>[<span class="n">1</span>] = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span></li></ol></pre><p>However, in list context, the line input operator returns all of the
lines as a list. The first line goes into <code class="inline"><span class="i">@array</span>[<span class="n">1</span>]</code>
 and the rest
of the lines mysteriously disappear:</p>
<pre class="verbatim"><ol><li>    <span class="i">@array</span>[<span class="n">1</span>] = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span>  <span class="c"># most likely not what you want</span></li></ol></pre><p>Either the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
 pragma or the <b>-w</b> flag will warn you when
you use an array slice with a single index.</p>
<a name="How-can-I-remove-duplicate-elements-from-a-list-or-array%3f"></a><h2>How can I remove duplicate elements from a list or array?</h2>
<p>(contributed by brian d foy)</p>
<p>Use a hash. When you think the words "unique" or "duplicated", think
"hash keys".</p>
<p>If you don't care about the order of the elements, you could just
create the hash then extract the keys. It's not important how you
create that hash: just that you use <code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code> to get the unique
elements.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%hash</span>   = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">1</span> <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li><li>    <span class="c"># or a hash slice: @hash{ @array } = ();</span></li><li>    <span class="c"># or a foreach: $hash{$_} = 1 foreach ( @array );</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@unique</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span></li></ol></pre><p>If you want to use a module, try the <code class="inline"><span class="w">uniq</span></code>
 function from
<a href="http://search.cpan.org/perldoc/List::MoreUtils">List::MoreUtils</a>. In list context it returns the unique elements,
preserving their order in the list. In scalar context, it returns the
number of unique elements.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">List::MoreUtils</span> <span class="q">qw(uniq)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@unique</span> = <span class="i">uniq</span><span class="s">(</span> <span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="cm">,</span> <span class="n">6</span><span class="cm">,</span> <span class="n">5</span><span class="cm">,</span> <span class="n">7</span> <span class="s">)</span><span class="sc">;</span> <span class="c"># 1,2,3,4,5,6,7</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$unique</span> = <span class="i">uniq</span><span class="s">(</span> <span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="cm">,</span> <span class="n">6</span><span class="cm">,</span> <span class="n">5</span><span class="cm">,</span> <span class="n">7</span> <span class="s">)</span><span class="sc">;</span> <span class="c"># 7</span></li></ol></pre><p>You can also go through each element and skip the ones you've seen
before. Use a hash to keep track. The first time the loop sees an
element, that element has no key in <code class="inline"><span class="i">%Seen</span></code>
. The <code class="inline"><a class="l_k" href="functions/next.html">next</a></code> statement
creates the key and immediately uses its value, which is <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>, so
the loop continues to the <code class="inline"><a class="l_k" href="functions/push.html">push</a></code> and increments the value for that
key. The next time the loop sees that same element, its key exists in
the hash <i>and</i> the value for that key is true (since it's not 0 or
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>), so the next skips that iteration and the loop goes to the
next element.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@unique</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%seen</span>   = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$elem</span> <span class="s">(</span> <span class="i">@array</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/next.html">next</a> <a class="l_k" href="functions/if.html">if</a> <span class="i">$seen</span>{ <span class="i">$elem</span> }++<span class="sc">;</span></li><li>        <a class="l_k" href="functions/push.html">push</a> <span class="i">@unique</span><span class="cm">,</span> <span class="i">$elem</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>You can write this more briefly using a grep, which does the
same thing.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%seen</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@unique</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> ! <span class="i">$seen</span>{ <span class="i">$_</span> }++ <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><a name="How-can-I-tell-whether-a-certain-element-is-contained-in-a-list-or-array%3f"></a><h2>How can I tell whether a certain element is contained in a list or array?</h2>
<p>(portions of this answer contributed by Anno Siegel and brian d foy)</p>
<p>Hearing the word "in" is an <i>in</i>dication that you probably should have
used a hash, not a list or array, to store your data. Hashes are
designed to answer this question quickly and efficiently. Arrays aren't.</p>
<p>That being said, there are several ways to approach this. In Perl 5.10
and later, you can use the smart match operator to check that an item is
contained in an array or a hash:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="n">5.010</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="i">$item</span> ~~ <span class="i">@array</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;The array contains $item&quot;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="i">$item</span> ~~ <span class="i">%hash</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;The hash contains $item&quot;</span></li><li>    <span class="s">}</span></li></ol></pre><p>With earlier versions of Perl, you have to do a bit more work. If you
are going to make this query many times over arbitrary string values,
the fastest way is probably to invert the original array and maintain a
hash whose keys are the first array's values:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@blues</span> = <span class="q">qw/azure cerulean teal turquoise lapis-lazuli/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%is_blue</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@blues</span><span class="s">)</span> <span class="s">{</span> <span class="i">$is_blue</span>{<span class="i">$_</span>} = <span class="n">1</span> <span class="s">}</span></li></ol></pre><p>Now you can check whether <code class="inline"><span class="i">$is_blue</span>{<span class="i">$some_color</span>}</code>
. It might have
been a good idea to keep the blues all in a hash in the first place.</p>
<p>If the values are all small integers, you could use a simple indexed
array. This kind of an array will take up less space:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@primes</span> = <span class="s">(</span><span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="cm">,</span> <span class="n">7</span><span class="cm">,</span> <span class="n">11</span><span class="cm">,</span> <span class="n">13</span><span class="cm">,</span> <span class="n">17</span><span class="cm">,</span> <span class="n">19</span><span class="cm">,</span> <span class="n">23</span><span class="cm">,</span> <span class="n">29</span><span class="cm">,</span> <span class="n">31</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@is_tiny_prime</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@primes</span><span class="s">)</span> <span class="s">{</span> <span class="i">$is_tiny_prime</span>[<span class="i">$_</span>] = <span class="n">1</span> <span class="s">}</span></li><li>    <span class="c"># or simply  @istiny_prime[@primes] = (1) x @primes;</span></li></ol></pre><p>Now you check whether $is_tiny_prime[$some_number].</p>
<p>If the values in question are integers instead of strings, you can save
quite a lot of space by using bit strings instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@articles</span> = <span class="s">(</span> <span class="n">1</span>..<span class="n">10</span><span class="cm">,</span> <span class="n">150</span>..<span class="n">2000</span><span class="cm">,</span> <span class="n">2017</span> <span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/undef.html">undef</a> <span class="i">$read</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@articles</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$read</span><span class="cm">,</span><span class="i">$_</span><span class="cm">,</span><span class="n">1</span><span class="s">)</span> = <span class="n">1</span> <span class="s">}</span></li></ol></pre><p>Now check whether <code class="inline"><a class="l_k" href="functions/vec.html">vec($read,$n,1)</a></code> is true for some <code class="inline"><span class="i">$n</span></code>
.</p>
<p>These methods guarantee fast individual tests but require a re-organization
of the original list or array. They only pay off if you have to test
multiple values against the same array.</p>
<p>If you are testing only once, the standard module <a href="List/Util.html">List::Util</a> exports
the function <code class="inline"><span class="w">first</span></code>
 for this purpose. It works by stopping once it
finds the element. It's written in C for speed, and its Perl equivalent
looks like this subroutine:</p>
<pre class="verbatim"><ol><li><a name="first"></a>    sub <span class="m">first (&amp;@)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$code</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span><span class="i">@_</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/return.html">return</a> <span class="i">$_</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">&amp;</span>{<span class="i">$code</span>}<span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>If speed is of little concern, the common idiom uses grep in scalar context
(which returns the number of items that passed its condition) to traverse the
entire list. This does have the benefit of telling you how many matches it
found, though.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$is_there</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="i">$_</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="i">$whatever</span><span class="cm">,</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>If you want to actually extract the matching elements, simply use grep in
list context.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@matches</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="i">$_</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="i">$whatever</span><span class="cm">,</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-compute-the-difference-of-two-arrays%3f-How-do-I-compute-the-intersection-of-two-arrays%3f"></a><h2>How do I compute the difference of two arrays? How do I compute the intersection of two arrays?</h2>
<p>Use a hash. Here's code to do both and more. It assumes that each
element is unique in a given array:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">@union</span><span class="cm">,</span> <span class="i">@intersection</span><span class="cm">,</span> <span class="i">@difference</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%count</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> <span class="s">(</span><span class="i">@array1</span><span class="cm">,</span> <span class="i">@array2</span><span class="s">)</span> <span class="s">{</span> <span class="i">$count</span>{<span class="i">$element</span>}++ <span class="s">}</span></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> <span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a> <span class="i">%count</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/push.html">push</a> <span class="i">@union</span><span class="cm">,</span> <span class="i">$element</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/push.html">push</a> <span class="i">@</span>{ <span class="i">$count</span>{<span class="i">$element</span>} &gt; <span class="n">1</span> ? \<span class="i">@intersection</span> <span class="co">:</span> \<span class="i">@difference</span> }<span class="cm">,</span> <span class="i">$element</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that this is the <i>symmetric difference</i>, that is, all elements
in either A or in B but not in both. Think of it as an xor operation.</p>
<a name="How-do-I-test-whether-two-arrays-or-hashes-are-equal%3f"></a><h2>How do I test whether two arrays or hashes are equal?</h2>
<p>With Perl 5.10 and later, the smart match operator can give you the answer
with the least amount of work:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="n">5.010</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="i">@array1</span> ~~ <span class="i">@array2</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;The arrays are the same&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="i">%hash1</span> ~~ <span class="i">%hash2</span> <span class="s">)</span> <span class="c"># doesn&#39;t check values!  {</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;The hash keys are the same&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>The following code works for single-level arrays. It uses a
stringwise comparison, and does not distinguish defined versus
undefined empty strings. Modify if you have other needs.</p>
<pre class="verbatim"><ol><li>    <span class="i">$are_equal</span> = <span class="i">compare_arrays</span><span class="s">(</span>\<span class="i">@frogs</span><span class="cm">,</span> \<span class="i">@toads</span><span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="compare_arrays"></a>    sub <span class="m">compare_arrays</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$first</span><span class="cm">,</span> <span class="i">$second</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span>  <span class="c"># silence spurious -w undef complaints</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="n">0</span> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">@$first</span> == <span class="i">@$second</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$i</span> &lt; <span class="i">@$first</span><span class="sc">;</span> <span class="i">$i</span>++<span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/return.html">return</a> <span class="n">0</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$first</span>-&gt;[<span class="i">$i</span>] <a class="l_k" href="functions/ne.html">ne</a> <span class="i">$second</span>-&gt;[<span class="i">$i</span>]<span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>For multilevel structures, you may wish to use an approach more
like this one. It uses the CPAN module <a href="http://search.cpan.org/perldoc/FreezeThaw">FreezeThaw</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">FreezeThaw</span> <span class="q">qw(cmpStr)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@a</span> = <a class="l_k" href="functions/my.html">my</a> <span class="i">@b</span> = <span class="s">(</span> <span class="q">&quot;this&quot;</span><span class="cm">,</span> <span class="q">&quot;that&quot;</span><span class="cm">,</span> <span class="s">[</span> <span class="q">&quot;more&quot;</span><span class="cm">,</span> <span class="q">&quot;stuff&quot;</span> <span class="s">]</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;a and b contain %s arrays\n&quot;</span><span class="cm">,</span></li><li>        <span class="i">cmpStr</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="n">0</span></li><li>        ? <span class="q">&quot;the same&quot;</span></li><li>        <span class="co">:</span> <span class="q">&quot;different&quot;</span><span class="sc">;</span></li></ol></pre><p>This approach also works for comparing hashes. Here we'll demonstrate
two different answers:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">FreezeThaw</span> <span class="q">qw(cmpStr cmpStrHard)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%a</span> = <a class="l_k" href="functions/my.html">my</a> <span class="i">%b</span> = <span class="s">(</span> <span class="q">&quot;this&quot;</span> <span class="cm">=&gt;</span> <span class="q">&quot;that&quot;</span><span class="cm">,</span> <span class="q">&quot;extra&quot;</span> <span class="cm">=&gt;</span> <span class="s">[</span> <span class="q">&quot;more&quot;</span><span class="cm">,</span> <span class="q">&quot;stuff&quot;</span> <span class="s">]</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$a</span>{<span class="w">EXTRA</span>} = \<span class="i">%b</span><span class="sc">;</span></li><li>    <span class="i">$b</span>{<span class="w">EXTRA</span>} = \<span class="i">%a</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;a and b contain %s hashes\n&quot;</span><span class="cm">,</span></li><li>    <span class="i">cmpStr</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="n">0</span> ? <span class="q">&quot;the same&quot;</span> <span class="co">:</span> <span class="q">&quot;different&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;a and b contain %s hashes\n&quot;</span><span class="cm">,</span></li><li>    <span class="i">cmpStrHard</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="n">0</span> ? <span class="q">&quot;the same&quot;</span> <span class="co">:</span> <span class="q">&quot;different&quot;</span><span class="sc">;</span></li></ol></pre><p>The first reports that both those the hashes contain the same data,
while the second reports that they do not. Which you prefer is left as
an exercise to the reader.</p>
<a name="How-do-I-find-the-first-array-element-for-which-a-condition-is-true%3f"></a><h2>How do I find the first array element for which a condition is true?</h2>
<p>To find the first array element which satisfies a condition, you can
use the <code class="inline"><span class="i">first</span><span class="s">(</span><span class="s">)</span></code>
 function in the <a href="List/Util.html">List::Util</a> module, which comes
with Perl 5.8. This example finds the first element that contains
"Perl".</p>
<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(first)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> = <span class="i">first</span> <span class="s">{</span> <span class="q">/Perl/</span> <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>If you cannot use <a href="List/Util.html">List::Util</a>, you can make your own loop to do the
same thing. Once you find the element, you stop the loop with last.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$found</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span> <span class="i">@array</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="q">/Perl/</span> <span class="s">)</span> <span class="s">{</span> <span class="i">$found</span> = <span class="i">$_</span><span class="sc">;</span> <a class="l_k" href="functions/last.html">last</a> <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>If you want the array index, use the <code class="inline"><span class="i">firstidx</span><span class="s">(</span><span class="s">)</span></code>
 function from
<code class="inline"><span class="w">List::MoreUtils</span></code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">List::MoreUtils</span> <span class="q">qw(firstidx)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$index</span> = <span class="i">firstidx</span> <span class="s">{</span> <span class="q">/Perl/</span> <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>Or write it yourself, iterating through the indices
and checking the array element at each index until you find one
that satisfies the condition:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a><span class="s">(</span> <span class="i">$found</span><span class="cm">,</span> <span class="i">$index</span> <span class="s">)</span> = <span class="s">(</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <span class="n">-1</span> <span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a><span class="s">(</span> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$i</span> &lt; <span class="i">@array</span><span class="sc">;</span> <span class="i">$i</span>++ <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="i">$array</span>[<span class="i">$i</span>] =~ <span class="q">/Perl/</span> <span class="s">)</span> <span class="s">{</span></li><li>            <span class="i">$found</span> = <span class="i">$array</span>[<span class="i">$i</span>]<span class="sc">;</span></li><li>            <span class="i">$index</span> = <span class="i">$i</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/last.html">last</a><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><a name="How-do-I-handle-linked-lists%3f"></a><h2>How do I handle linked lists?</h2>
<p>(contributed by brian d foy)</p>
<p>Perl's arrays do not have a fixed size, so you don't need linked lists
if you just want to add or remove items. You can use array operations
such as <code class="inline"><a class="l_k" href="functions/push.html">push</a></code>, <code class="inline"><a class="l_k" href="functions/pop.html">pop</a></code>, <code class="inline"><a class="l_k" href="functions/shift.html">shift</a></code>, <code class="inline"><a class="l_k" href="functions/unshift.html">unshift</a></code>, or <code class="inline"><a class="l_k" href="functions/splice.html">splice</a></code> to do
that.</p>
<p>Sometimes, however, linked lists can be useful in situations where you
want to "shard" an array so you have many small arrays instead of
a single big array. You can keep arrays longer than Perl's largest
array index, lock smaller arrays separately in threaded programs,
reallocate less memory, or quickly insert elements in the middle of
the chain.</p>
<p>Steve Lembark goes through the details in his YAPC::NA 2009 talk "Perly
Linked Lists" ( <a href="http://www.slideshare.net/lembark/perly-linked-lists">http://www.slideshare.net/lembark/perly-linked-lists</a> ),
although you can just use his <a href="http://search.cpan.org/perldoc/LinkedList::Single">LinkedList::Single</a> module.</p>
<a name="How-do-I-handle-circular-lists%3f"></a><h2>How do I handle circular lists?
   
 </h2>
<p>(contributed by brian d foy)</p>
<p>If you want to cycle through an array endlessly, you can increment the
index modulo the number of elements in the array:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@array</span> = <span class="q">qw( a b c )</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/while.html">while</a><span class="s">(</span> <span class="n">1</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$array</span>[ <span class="i">$i</span>++ % <span class="i">@array</span> ]<span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/last.html">last</a> <a class="l_k" href="functions/if.html">if</a> <span class="i">$i</span> &gt; <span class="n">20</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>You can also use <a href="http://search.cpan.org/perldoc/Tie::Cycle">Tie::Cycle</a> to use a scalar that always has the
next element of the circular array:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Tie::Cycle</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$cycle</span><span class="cm">,</span> <span class="q">&#39;Tie::Cycle&#39;</span><span class="cm">,</span> <span class="s">[</span> <span class="q">qw( FFFFFF 000000 FFFF00 )</span> <span class="s">]</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$cycle</span><span class="sc">;</span> <span class="c"># FFFFFF</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$cycle</span><span class="sc">;</span> <span class="c"># 000000</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$cycle</span><span class="sc">;</span> <span class="c"># FFFF00</span></li></ol></pre><p>The <a href="http://search.cpan.org/perldoc/Array::Iterator::Circular">Array::Iterator::Circular</a> creates an iterator object for
circular arrays:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Array::Iterator::Circular</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$color_iterator</span> = <span class="w">Array::Iterator::Circular</span><span class="w">-&gt;new</span><span class="s">(</span></li><li>        <span class="q">qw(red green blue orange)</span></li><li>        <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span> <span class="n">1</span> .. <span class="n">20</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$color_iterator</span><span class="i">-&gt;next</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><a name="How-do-I-shuffle-an-array-randomly%3f"></a><h2>How do I shuffle an array randomly?</h2>
<p>If you either have Perl 5.8.0 or later installed, or if you have
Scalar-List-Utils 1.03 or later installed, you can say:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">List::Util</span> <span class="q">&#39;shuffle&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="i">@shuffled</span> = <span class="i">shuffle</span><span class="s">(</span><span class="i">@list</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If not, you can use a Fisher-Yates shuffle.</p>
<pre class="verbatim"><ol><li><a name="fisher_yates_shuffle"></a>    sub <span class="m">fisher_yates_shuffle</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$deck</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span>  <span class="c"># $deck is a reference to an array</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">@$deck</span><span class="sc">;</span> <span class="c"># must not be empty!</span></li><li></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="i">@$deck</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span>--<span class="i">$i</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">$j</span> = <a class="l_k" href="functions/int.html">int</a> <a class="l_k" href="functions/rand.html">rand</a> <span class="s">(</span><span class="i">$i</span>+<span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>            <span class="i">@$deck</span>[<span class="i">$i</span><span class="cm">,</span><span class="i">$j</span>] = <span class="i">@$deck</span>[<span class="i">$j</span><span class="cm">,</span><span class="i">$i</span>]<span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># shuffle my mpeg collection</span></li><li>    <span class="c">#</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@mpeg</span> = <span class="q">&lt;audio/*/*.mp3&gt;</span><span class="sc">;</span></li><li>    <span class="i">fisher_yates_shuffle</span><span class="s">(</span> \<span class="i">@mpeg</span> <span class="s">)</span><span class="sc">;</span>    <span class="c"># randomize @mpeg in place</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">@mpeg</span><span class="sc">;</span></li></ol></pre><p>Note that the above implementation shuffles an array in place,
unlike the <code class="inline"><span class="i">List::Util::shuffle</span><span class="s">(</span><span class="s">)</span></code>
 which takes a list and returns
a new shuffled list.</p>
<p>You've probably seen shuffling algorithms that work using splice,
randomly picking another element to swap the current element with</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/srand.html">srand</a><span class="sc">;</span></li><li>    <span class="i">@new</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@old</span> = <span class="n">1</span> .. <span class="n">10</span><span class="sc">;</span>  <span class="c"># just a demo</span></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="i">@old</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@new</span><span class="cm">,</span> <a class="l_k" href="functions/splice.html">splice</a><span class="s">(</span><span class="i">@old</span><span class="cm">,</span> <a class="l_k" href="functions/rand.html">rand</a> <span class="i">@old</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>This is bad because splice is already O(N), and since you do it N
times, you just invented a quadratic algorithm; that is, O(N**2).
This does not scale, although Perl is so efficient that you probably
won't notice this until you have rather largish arrays.</p>
<a name="How-do-I-process%2fmodify-each-element-of-an-array%3f"></a><h2>How do I process/modify each element of an array?</h2>
<p>Use <code class="inline">for</code>
/<code class="inline">foreach</code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@lines</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="q">s/foo/bar/</span><span class="sc">;</span>    <span class="c"># change that word</span></li><li>        <span class="q">tr/XZ/ZX/</span><span class="sc">;</span>    <span class="c"># swap those letters</span></li><li>    <span class="s">}</span></li></ol></pre><p>Here's another; let's compute spherical volumes:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@volumes</span> = <span class="i">@radii</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@volumes</span><span class="s">)</span> <span class="s">{</span>   <span class="c"># @volumes has changed parts</span></li><li>        <span class="i">$_</span> **= <span class="n">3</span><span class="sc">;</span></li><li>        <span class="i">$_</span> *= <span class="s">(</span><span class="n">4</span>/<span class="n">3</span><span class="s">)</span> * <span class="n">3.14159</span><span class="sc">;</span>  <span class="c"># this will be constant folded</span></li><li>    <span class="s">}</span></li></ol></pre><p>which can also be done with <code class="inline"><a class="l_k" href="functions/map.html">map()</a></code> which is made to transform
one list into another:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@volumes</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span><span class="i">$_</span> ** <span class="n">3</span> * <span class="s">(</span><span class="n">4</span>/<span class="n">3</span><span class="s">)</span> * <span class="n">3.14159</span><span class="s">}</span> <span class="i">@radii</span><span class="sc">;</span></li></ol></pre><p>If you want to do the same thing to modify the values of the
hash, you can use the <code class="inline"><a class="l_k" href="functions/values.html">values</a></code> function. As of Perl 5.6
the values are not copied, so if you modify $orbit (in this
case), you modify the value.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$orbit</span> <span class="s">(</span> <a class="l_k" href="functions/values.html">values</a> <span class="i">%orbits</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="s">(</span><span class="i">$orbit</span> **= <span class="n">3</span><span class="s">)</span> *= <span class="s">(</span><span class="n">4</span>/<span class="n">3</span><span class="s">)</span> * <span class="n">3.14159</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Prior to perl 5.6 <code class="inline"><a class="l_k" href="functions/values.html">values</a></code> returned copies of the values,
so older perl code often contains constructions such as
<code class="inline"><span class="i">@orbits</span>{<a class="l_k" href="functions/keys.html">keys</a> <span class="i">%orbits</span>}</code>
 instead of <code class="inline"><a class="l_k" href="functions/values.html">values</a> <span class="i">%orbits</span></code>
 where
the hash is to be modified.</p>
<a name="How-do-I-select-a-random-element-from-an-array%3f"></a><h2>How do I select a random element from an array?</h2>
<p>Use the <code class="inline"><a class="l_k" href="functions/rand.html">rand()</a></code> function (see <a href="functions/rand.html">rand</a>):</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$index</span>   = <a class="l_k" href="functions/rand.html">rand</a> <span class="i">@array</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> = <span class="i">$array</span>[<span class="i">$index</span>]<span class="sc">;</span></li></ol></pre><p>Or, simply:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> = <span class="i">$array</span>[ <a class="l_k" href="functions/rand.html">rand</a> <span class="i">@array</span> ]<span class="sc">;</span></li></ol></pre><a name="How-do-I-permute-N-elements-of-a-list%3f"></a><h2>How do I permute N elements of a list?
   
 </h2>
<p>Use the <a href="http://search.cpan.org/perldoc/List::Permutor">List::Permutor</a> module on CPAN. If the list is actually an
array, try the <a href="http://search.cpan.org/perldoc/Algorithm::Permute">Algorithm::Permute</a> module (also on CPAN). It's
written in XS code and is very efficient:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Algorithm::Permute</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@array</span> = <span class="q">&#39;a&#39;</span>..<span class="q">&#39;d&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$p_iterator</span> = <span class="w">Algorithm::Permute</span><span class="w">-&gt;new</span> <span class="s">(</span> \<span class="i">@array</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">@perm</span> = <span class="i">$p_iterator</span><span class="i">-&gt;next</span><span class="s">)</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;next permutation: (@perm)\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>For even faster execution, you could do:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Algorithm::Permute</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@array</span> = <span class="q">&#39;a&#39;</span>..<span class="q">&#39;d&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="i">Algorithm::Permute::permute</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;next permutation: (@array)\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>Here's a little program that generates all permutations of all the
words on each line of input. The algorithm embodied in the
<code class="inline"><span class="i">permute</span><span class="s">(</span><span class="s">)</span></code>
 function is discussed in Volume 4 (still unpublished) of
Knuth's <i>The Art of Computer Programming</i> and will work on any list:</p>
<pre class="verbatim"><ol><li>    <span class="c">#!/usr/bin/perl -n</span></li><li>    <span class="c"># Fischer-Krause ordered permutation generator</span></li><li></li><li><a name="permute"></a>    sub <span class="m">permute (&amp;@)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$code</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">@idx</span> = <span class="n">0</span>..<span class="i">$#_</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span> <span class="i">$code</span>-&gt;<span class="s">(</span><span class="i">@_</span>[<span class="i">@idx</span>]<span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">$p</span> = <span class="i">$#idx</span><span class="sc">;</span></li><li>            --<span class="i">$p</span> <a class="l_k" href="functions/while.html">while</a> <span class="i">$idx</span>[<span class="i">$p</span>-<span class="n">1</span>] &gt; <span class="i">$idx</span>[<span class="i">$p</span>]<span class="sc">;</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">$q</span> = <span class="i">$p</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/return.html">return</a><span class="sc">;</span></li><li>            <a class="l_k" href="functions/push.html">push</a> <span class="i">@idx</span><span class="cm">,</span> <a class="l_k" href="functions/reverse.html">reverse</a> <a class="l_k" href="functions/splice.html">splice</a> <span class="i">@idx</span><span class="cm">,</span> <span class="i">$p</span><span class="sc">;</span></li><li>            ++<span class="i">$q</span> <a class="l_k" href="functions/while.html">while</a> <span class="i">$idx</span>[<span class="i">$p</span>-<span class="n">1</span>] &gt; <span class="i">$idx</span>[<span class="i">$q</span>]<span class="sc">;</span></li><li>            <span class="i">@idx</span>[<span class="i">$p</span>-<span class="n">1</span><span class="cm">,</span><span class="i">$q</span>]=<span class="i">@idx</span>[<span class="i">$q</span><span class="cm">,</span><span class="i">$p</span>-<span class="n">1</span>]<span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">permute</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;@_\n&quot;</span> <span class="s">}</span> <a class="l_k" href="functions/split.html">split</a><span class="sc">;</span></li></ol></pre><p>The <a href="http://search.cpan.org/perldoc/Algorithm::Loops">Algorithm::Loops</a> module also provides the <code class="inline"><span class="w">NextPermute</span></code>
 and
<code class="inline"><span class="w">NextPermuteNum</span></code>
 functions which efficiently find all unique permutations
of an array, even if it contains duplicate values, modifying it in-place:
if its elements are in reverse-sorted order then the array is reversed,
making it sorted, and it returns false; otherwise the next
permutation is returned.</p>
<p><code class="inline"><span class="w">NextPermute</span></code>
 uses string order and <code class="inline"><span class="w">NextPermuteNum</span></code>
 numeric order, so
you can enumerate all the permutations of <code class="inline"><span class="n">0</span>..<span class="n">9</span></code>
 like this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Algorithm::Loops</span> <span class="q">qw(NextPermuteNum)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@list</span>= <span class="n">0</span>..<span class="n">9</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;@list\n&quot;</span> <span class="s">}</span> <a class="l_k" href="functions/while.html">while</a> <span class="w">NextPermuteNum</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-sort-an-array-by-(anything)%3f"></a><h2>How do I sort an array by (anything)?</h2>
<p>Supply a comparison function to sort() (described in <a href="functions/sort.html">sort</a>):</p>
<pre class="verbatim"><ol><li>    <span class="i">@list</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> &lt;=&gt; <span class="i">$b</span> <span class="s">}</span> <span class="i">@list</span><span class="sc">;</span></li></ol></pre><p>The default sort function is cmp, string comparison, which would
sort <code class="inline"><span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">10</span><span class="s">)</span></code>
 into <code class="inline"><span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">10</span><span class="cm">,</span> <span class="n">2</span><span class="s">)</span></code>
. <code class="inline">&lt;=&gt;</code>
, used above, is
the numerical comparison operator.</p>
<p>If you have a complicated function needed to pull out the part you
want to sort on, then don't do it inside the sort function. Pull it
out first, because the sort BLOCK can be called many times for the
same element. Here's an example of how to pull out the first word
after the first number on each item, and then sort those words
case-insensitively.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@idx</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@data</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$item</span><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$item</span><span class="s">)</span> = <span class="q">/\d+\s*(\S+)/</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/push.html">push</a> <span class="i">@idx</span><span class="cm">,</span> <a class="l_k" href="functions/uc.html">uc</a><span class="s">(</span><span class="i">$item</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@sorted</span> = <span class="i">@data</span>[ <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$idx</span>[<span class="i">$a</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$idx</span>[<span class="i">$b</span>] <span class="s">}</span> <span class="n">0</span> .. <span class="i">$#idx</span> ]<span class="sc">;</span></li></ol></pre><p>which could also be written this way, using a trick
that's come to be known as the Schwartzian Transform:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@sorted</span> = <a class="l_k" href="functions/map.html">map</a>  <span class="s">{</span> <span class="i">$_</span>-&gt;[<span class="n">0</span>] <span class="s">}</span></li><li>        <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span>-&gt;[<span class="n">1</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">1</span>] <span class="s">}</span></li><li>        <a class="l_k" href="functions/map.html">map</a>  <span class="s">{</span> <span class="s">[</span> <span class="i">$_</span><span class="cm">,</span> <a class="l_k" href="functions/uc.html">uc</a><span class="s">(</span> <span class="s">(</span><span class="q">/\d+\s*(\S+)/</span><span class="s">)</span>[<span class="n">0</span>]<span class="s">)</span> <span class="s">]</span> <span class="s">}</span> <span class="i">@data</span><span class="sc">;</span></li></ol></pre><p>If you need to sort on several fields, the following paradigm is useful.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@sorted</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span></li><li>        <span class="i">field1</span><span class="s">(</span><span class="i">$a</span><span class="s">)</span> &lt;=&gt; <span class="i">field1</span><span class="s">(</span><span class="i">$b</span><span class="s">)</span> ||</li><li>        <span class="i">field2</span><span class="s">(</span><span class="i">$a</span><span class="s">)</span> <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">field2</span><span class="s">(</span><span class="i">$b</span><span class="s">)</span> ||</li><li>        <span class="i">field3</span><span class="s">(</span><span class="i">$a</span><span class="s">)</span> <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">field3</span><span class="s">(</span><span class="i">$b</span><span class="s">)</span></li><li>    <span class="s">}</span> <span class="i">@data</span><span class="sc">;</span></li></ol></pre><p>This can be conveniently combined with precalculation of keys as given
above.</p>
<p>See the <i>sort</i> article in the "Far More Than You Ever Wanted
To Know" collection in <a href="http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz">http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz</a> for
more about this approach.</p>
<p>See also the question later in <a href="perlfaq4.html">perlfaq4</a> on sorting hashes.</p>
<a name="How-do-I-manipulate-arrays-of-bits%3f"></a><h2>How do I manipulate arrays of bits?</h2>
<p>Use <code class="inline"><a class="l_k" href="functions/pack.html">pack()</a></code> and <code class="inline"><a class="l_k" href="functions/unpack.html">unpack()</a></code>, or else <code class="inline"><a class="l_k" href="functions/vec.html">vec()</a></code> and the bitwise
operations.</p>
<p>For example, you don't have to store individual bits in an array
(which would mean that you're wasting a lot of space). To convert an
array of bits to a string, use <code class="inline"><a class="l_k" href="functions/vec.html">vec()</a></code> to set the right bits. This
sets <code class="inline"><span class="i">$vec</span></code>
 to have bit N set only if <code class="inline"><span class="i">$ints</span>[<span class="w">N</span>]</code>
 was set:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@ints</span> = <span class="s">(</span>...<span class="s">)</span><span class="sc">;</span> <span class="c"># array of bits, e.g. ( 1, 0, 0, 1, 1, 0 ... )</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a><span class="s">(</span> <span class="n">0</span> .. <span class="i">$#ints</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span><span class="i">$_</span><span class="cm">,</span><span class="n">1</span><span class="s">)</span> = <span class="n">1</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$ints</span>[<span class="i">$_</span>]<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>The string <code class="inline"><span class="i">$vec</span></code>
 only takes up as many bits as it needs. For
instance, if you had 16 entries in <code class="inline"><span class="i">@ints</span></code>
, <code class="inline"><span class="i">$vec</span></code>
 only needs two
bytes to store them (not counting the scalar variable overhead).</p>
<p>Here's how, given a vector in <code class="inline"><span class="i">$vec</span></code>
, you can get those bits into
your <code class="inline"><span class="i">@ints</span></code>
 array:</p>
<pre class="verbatim"><ol><li><a name="bitvec_to_list"></a>    sub <span class="m">bitvec_to_list</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$vec</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">@ints</span><span class="sc">;</span></li><li>        <span class="c"># Find null-byte density then select best algorithm</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$vec</span> =~ <span class="q">tr/\0//</span> / <a class="l_k" href="functions/length.html">length</a> <span class="i">$vec</span> &gt; <span class="n">0.95</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span><span class="sc">;</span></li><li></li><li>            <span class="c"># This method is faster with mostly null-bytes</span></li><li>            <a class="l_k" href="functions/while.html">while</a><span class="s">(</span><span class="i">$vec</span> =~ <span class="q">/[^\0]/g</span> <span class="s">)</span> <span class="s">{</span></li><li>                <span class="i">$i</span> = <span class="n">-9</span> + <span class="n">8</span> * <a class="l_k" href="functions/pos.html">pos</a> <span class="i">$vec</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>                <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="i">$i</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> ++<span class="i">$i</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>            <span class="s">}</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>            <span class="c"># This method is a fast general algorithm</span></li><li>            <a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">$bits</span> = <a class="l_k" href="functions/unpack.html">unpack</a> <span class="q">&quot;b*&quot;</span><span class="cm">,</span> <span class="i">$vec</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <span class="n">0</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$bits</span> =~ <span class="q">s/^(\d)//</span> &amp;&amp; <span class="i">$1</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <a class="l_k" href="functions/pos.html">pos</a> <span class="i">$bits</span> <a class="l_k" href="functions/while.html">while</a><span class="s">(</span><span class="i">$bits</span> =~ <span class="q">/1/g</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li></li><li>        <a class="l_k" href="functions/return.html">return</a> \<span class="i">@ints</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>This method gets faster the more sparse the bit vector is.
(Courtesy of Tim Bunce and Winfried Koenig.)</p>
<p>You can make the while loop a lot shorter with this suggestion
from Benjamin Goldberg:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a><span class="s">(</span><span class="i">$vec</span> =~ <span class="q">/[^\0]+/g</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/push.html">push</a> <span class="i">@ints</span><span class="cm">,</span> <a class="l_k" href="functions/grep.html">grep</a> <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vec</span><span class="cm">,</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="cm">,</span> <span class="i">$-</span>[<span class="n">0</span>] * <span class="n">8</span> .. <span class="i">$+</span>[<span class="n">0</span>] * <span class="n">8</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Or use the CPAN module <a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vector</span> = <span class="w">Bit::Vector</span><span class="w">-&gt;new</span><span class="s">(</span><span class="i">$num_of_bits</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$vector</span><span class="i">-&gt;Index_List_Store</span><span class="s">(</span><span class="i">@ints</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@ints</span> = <span class="i">$vector</span><span class="i">-&gt;Index_List_Read</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><a href="http://search.cpan.org/perldoc/Bit::Vector">Bit::Vector</a> provides efficient methods for bit vector, sets of
small integers and "big int" math.</p>
<p>Here's a more extensive illustration using vec():</p>
<pre class="verbatim"><ol><li>    <span class="c"># vec demo</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vector</span> = <span class="q">&quot;\xff\x0f\xef\xfe&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Ilya&#39;s string \\xff\\x0f\\xef\\xfe represents the number &quot;</span><span class="cm">,</span></li><li>    <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;N&quot;</span><span class="cm">,</span> <span class="i">$vector</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$is_set</span> = <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vector</span><span class="cm">,</span> <span class="n">23</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Its 23rd bit is &quot;</span><span class="cm">,</span> <span class="i">$is_set</span> ? <span class="q">&quot;set&quot;</span> <span class="co">:</span> <span class="q">&quot;clear&quot;</span><span class="cm">,</span> <span class="q">&quot;.\n&quot;</span><span class="sc">;</span></li><li>    <span class="i">pvec</span><span class="s">(</span><span class="i">$vector</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">1</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">23</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">2</span><span class="cm">,</span><span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">4</span><span class="cm">,</span><span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">4</span><span class="cm">,</span><span class="n">7</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">8</span><span class="cm">,</span><span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">8</span><span class="cm">,</span><span class="n">7</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">0</span><span class="cm">,</span><span class="n">32</span><span class="cm">,</span><span class="n">17</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">set_vec</span><span class="s">(</span><span class="n">1</span><span class="cm">,</span><span class="n">32</span><span class="cm">,</span><span class="n">17</span><span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="set_vec"></a>    sub <span class="m">set_vec</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$offset</span><span class="cm">,</span> <span class="i">$width</span><span class="cm">,</span> <span class="i">$value</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$vector</span> = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/vec.html">vec</a><span class="s">(</span><span class="i">$vector</span><span class="cm">,</span> <span class="i">$offset</span><span class="cm">,</span> <span class="i">$width</span><span class="s">)</span> = <span class="i">$value</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;offset=$offset width=$width value=$value\n&quot;</span><span class="sc">;</span></li><li>        <span class="i">pvec</span><span class="s">(</span><span class="i">$vector</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="pvec"></a>    sub <span class="m">pvec</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$vector</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$bits</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;b*&quot;</span><span class="cm">,</span> <span class="i">$vector</span><span class="s">)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$BASE</span> = <span class="n">8</span><span class="sc">;</span></li><li></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;vector length in bytes: &quot;</span><span class="cm">,</span> <a class="l_k" href="functions/length.html">length</a><span class="s">(</span><span class="i">$vector</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>        <span class="i">@bytes</span> = <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;A8&quot;</span> x <a class="l_k" href="functions/length.html">length</a><span class="s">(</span><span class="i">$vector</span><span class="s">)</span><span class="cm">,</span> <span class="i">$bits</span><span class="s">)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;bits are: @bytes\n\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><a name="Why-does-defined()-return-true-on-empty-arrays-and-hashes%3f"></a><h2>Why does defined() return true on empty arrays and hashes?</h2>
<p>The short story is that you should probably only use defined on scalars or
functions, not on aggregates (arrays and hashes). See <a href="functions/defined.html">defined</a>
in the 5.004 release or later of Perl for more detail.</p>
<a name="Data%3a-Hashes-(Associative-Arrays)"></a><h1>Data: Hashes (Associative Arrays)</h1>
<a name="How-do-I-process-an-entire-hash%3f"></a><h2>How do I process an entire hash?</h2>
<p>(contributed by brian d foy)</p>
<p>There are a couple of ways that you can process an entire hash. You
can get a list of keys, then go through each key, or grab a one
key-value pair at a time.</p>
<p>To go through all of the keys, use the <code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code> function. This extracts
all of the keys of the hash and gives them back to you as a list. You
can then get the value through the particular key you're processing:</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> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$value</span> = <span class="i">$hash</span>{<span class="i">$key</span>}</li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>Once you have the list of keys, you can process that list before you
process the hash elements. For instance, you can sort the keys so you
can process them in lexical order:</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> <a class="l_k" href="functions/sort.html">sort</a> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$value</span> = <span class="i">$hash</span>{<span class="i">$key</span>}</li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>Or, you might want to only process some of the items. If you only want
to deal with the keys that start with <code class="inline"><span class="j">text:</span></code>
, you can select just
those using <code class="inline"><a class="l_k" href="functions/grep.html">grep</a></code>:</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> <a class="l_k" href="functions/grep.html">grep</a> <span class="q">/^text:/</span><span class="cm">,</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$value</span> = <span class="i">$hash</span>{<span class="i">$key</span>}</li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>If the hash is very large, you might not want to create a long list of
keys. To save some memory, you can grab one key-value pair at a time using
<code class="inline"><a class="l_k" href="functions/each.html">each()</a></code>, which returns a pair you haven't seen yet:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a><span class="s">(</span> <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> = <a class="l_k" href="functions/each.html">each</a><span class="s">(</span> <span class="i">%hash</span> <span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/each.html">each</a></code> operator returns the pairs in apparently random order, so if
ordering matters to you, you'll have to stick with the <code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code> method.</p>
<p>The <code class="inline"><a class="l_k" href="functions/each.html">each()</a></code> operator can be a bit tricky though. You can't add or
delete keys of the hash while you're using it without possibly
skipping or re-processing some pairs after Perl internally rehashes
all of the elements. Additionally, a hash has only one iterator, so if
you mix <code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code>, <code class="inline"><a class="l_k" href="functions/values.html">values</a></code>, or <code class="inline"><a class="l_k" href="functions/each.html">each</a></code> on the same hash, you risk resetting
the iterator and messing up your processing. See the <code class="inline"><a class="l_k" href="functions/each.html">each</a></code> entry in
<a href="perlfunc.html">perlfunc</a> for more details.</p>
<a name="How-do-I-merge-two-hashes%3f"></a><h2>How do I merge two hashes?
  </h2>
<p>(contributed by brian d foy)</p>
<p>Before you decide to merge two hashes, you have to decide what to do
if both hashes contain keys that are the same and if you want to leave
the original hashes as they were.</p>
<p>If you want to preserve the original hashes, copy one hash (<code class="inline"><span class="i">%hash1</span></code>
)
to a new hash (<code class="inline"><span class="i">%new_hash</span></code>
), then add the keys from the other hash
(<code class="inline"><span class="i">%hash2</span></code>
 to the new hash. Checking that the key already exists in
<code class="inline"><span class="i">%new_hash</span></code>
 gives you a chance to decide what to do with the
duplicates:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%new_hash</span> = <span class="i">%hash1</span><span class="sc">;</span> <span class="c"># make a copy; leave %hash1 alone</span></li><li></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$key2</span> <span class="s">(</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash2</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$new_hash</span>{<span class="i">$key2</span>} <span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/warn.html">warn</a> <span class="q">&quot;Key [$key2] is in both hashes!&quot;</span><span class="sc">;</span></li><li>            <span class="c"># handle the duplicate (perhaps only warning)</span></li><li>            ...</li><li>            <a class="l_k" href="functions/next.html">next</a><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>            <span class="i">$new_hash</span>{<span class="i">$key2</span>} = <span class="i">$hash2</span>{<span class="i">$key2</span>}<span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>If you don't want to create a new hash, you can still use this looping
technique; just change the <code class="inline"><span class="i">%new_hash</span></code>
 to <code class="inline"><span class="i">%hash1</span></code>
.</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">$key2</span> <span class="s">(</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash2</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash1</span>{<span class="i">$key2</span>} <span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/warn.html">warn</a> <span class="q">&quot;Key [$key2] is in both hashes!&quot;</span><span class="sc">;</span></li><li>            <span class="c"># handle the duplicate (perhaps only warning)</span></li><li>            ...</li><li>            <a class="l_k" href="functions/next.html">next</a><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>            <span class="i">$hash1</span>{<span class="i">$key2</span>} = <span class="i">$hash2</span>{<span class="i">$key2</span>}<span class="sc">;</span></li><li>        <span class="s">}</span></li><li>      <span class="s">}</span></li></ol></pre><p>If you don't care that one hash overwrites keys and values from the other, you
could just use a hash slice to add one hash to another. In this case, values
from <code class="inline"><span class="i">%hash2</span></code>
 replace values from <code class="inline"><span class="i">%hash1</span></code>
 when they have keys in common:</p>
<pre class="verbatim"><ol><li>    <span class="i">@hash1</span>{ <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash2</span> } = <a class="l_k" href="functions/values.html">values</a> <span class="i">%hash2</span><span class="sc">;</span></li></ol></pre><a name="What-happens-if-I-add-or-remove-keys-from-a-hash-while-iterating-over-it%3f"></a><h2>What happens if I add or remove keys from a hash while iterating over it?</h2>
<p>(contributed by brian d foy)</p>
<p>The easy answer is "Don't do that!"</p>
<p>If you iterate through the hash with each(), you can delete the key
most recently returned without worrying about it. If you delete or add
other keys, the iterator may skip or double up on them since perl
may rearrange the hash table. See the
entry for <code class="inline"><a class="l_k" href="functions/each.html">each()</a></code> in <a href="perlfunc.html">perlfunc</a>.</p>
<a name="How-do-I-look-up-a-hash-element-by-value%3f"></a><h2>How do I look up a hash element by value?</h2>
<p>Create a reverse hash:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%by_value</span> = <a class="l_k" href="functions/reverse.html">reverse</a> <span class="i">%by_key</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> = <span class="i">$by_value</span>{<span class="i">$value</span>}<span class="sc">;</span></li></ol></pre><p>That's not particularly efficient. It would be more space-efficient
to use:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><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> = <a class="l_k" href="functions/each.html">each</a> <span class="i">%by_key</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$by_value</span>{<span class="i">$value</span>} = <span class="i">$key</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>If your hash could have repeated values, the methods above will only find
one of the associated keys.  This may or may not worry you. If it does
worry you, you can always reverse the hash into a hash of arrays instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><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> = <a class="l_k" href="functions/each.html">each</a> <span class="i">%by_key</span><span class="s">)</span> <span class="s">{</span></li><li>         <a class="l_k" href="functions/push.html">push</a> <span class="i">@</span>{<span class="i">$key_list_by_value</span>{<span class="i">$value</span>}}<span class="cm">,</span> <span class="i">$key</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><a name="How-can-I-know-how-many-entries-are-in-a-hash%3f"></a><h2>How can I know how many entries are in a hash?</h2>
<p>(contributed by brian d foy)</p>
<p>This is very similar to "How do I process an entire hash?", also in
<a href="perlfaq4.html">perlfaq4</a>, but a bit simpler in the common cases.</p>
<p>You can use the <code class="inline"><a class="l_k" href="functions/keys.html">keys()</a></code> built-in function in scalar context to find out
have many entries you have in a hash:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$key_count</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span> <span class="c"># must be scalar context!</span></li></ol></pre><p>If you want to find out how many entries have a defined value, that's
a bit different. You have to check each value. A <code class="inline"><a class="l_k" href="functions/grep.html">grep</a></code> is handy:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$defined_value_count</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> <a class="l_k" href="functions/defined.html">defined</a> <span class="s">}</span> <a class="l_k" href="functions/values.html">values</a> <span class="i">%hash</span><span class="sc">;</span></li></ol></pre><p>You can use that same structure to count the entries any way that
you like. If you want the count of the keys with vowels in them,
you just test for that instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$vowel_count</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> <span class="q">/[aeiou]/</span> <span class="s">}</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/grep.html">grep</a></code> in scalar context returns the count. If you want the list
of matching items, just use it in list context instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@defined_values</span> = <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> <a class="l_k" href="functions/defined.html">defined</a> <span class="s">}</span> <a class="l_k" href="functions/values.html">values</a> <span class="i">%hash</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/keys.html">keys()</a></code> function also resets the iterator, which means that you may
see strange results if you use this between uses of other hash operators
such as <code class="inline"><a class="l_k" href="functions/each.html">each()</a></code>.</p>
<a name="How-do-I-sort-a-hash-(optionally-by-value-instead-of-key)%3f"></a><h2>How do I sort a hash (optionally by value instead of key)?</h2>
<p>(contributed by brian d foy)</p>
<p>To sort a hash, start with the keys. In this example, we give the list of
keys to the sort function which then compares them ASCIIbetically (which
might be affected by your locale settings). The output list has the keys
in ASCIIbetical order. Once we have the keys, we can go through them to
create a report which lists the keys in ASCIIbetical order.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@keys</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span> <span class="s">}</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span></li><li></li><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="i">@keys</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;%-20s %6d\n&quot;</span><span class="cm">,</span> <span class="i">$key</span><span class="cm">,</span> <span class="i">$hash</span>{<span class="i">$key</span>}<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>We could get more fancy in the <code class="inline"><a class="l_k" href="functions/sort.html">sort()</a></code> block though. Instead of
comparing the keys, we can compute a value with them and use that
value as the comparison.</p>
<p>For instance, to make our report order case-insensitive, we use
<code class="inline"><a class="l_k" href="functions/lc.html">lc</a></code> to lowercase the keys before comparing them:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@keys</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span> <a class="l_k" href="functions/lc.html">lc</a> <span class="i">$a</span> <a class="l_k" href="functions/cmp.html">cmp</a> <a class="l_k" href="functions/lc.html">lc</a> <span class="i">$b</span> <span class="s">}</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span></li></ol></pre><p>Note: if the computation is expensive or the hash has many elements,
you may want to look at the Schwartzian Transform to cache the
computation results.</p>
<p>If we want to sort by the hash value instead, we use the hash key
to look it up. We still get out a list of keys, but this time they
are ordered by their value.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@keys</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$hash</span>{<span class="i">$a</span>} &lt;=&gt; <span class="i">$hash</span>{<span class="i">$b</span>} <span class="s">}</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span></li></ol></pre><p>From there we can get more complex. If the hash values are the same,
we can provide a secondary sort on the hash key.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@keys</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span></li><li>        <span class="i">$hash</span>{<span class="i">$a</span>} &lt;=&gt; <span class="i">$hash</span>{<span class="i">$b</span>}</li><li>            <a class="l_k" href="functions/or.html">or</a></li><li>        <span class="q">&quot;\L$a&quot;</span> <a class="l_k" href="functions/cmp.html">cmp</a> <span class="q">&quot;\L$b&quot;</span></li><li>    <span class="s">}</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span></li></ol></pre><a name="How-can-I-always-keep-my-hash-sorted%3f"></a><h2>How can I always keep my hash sorted?
</h2>
<p>You can look into using the <code class="inline"><span class="w">DB_File</span></code>
 module and <code class="inline"><a class="l_k" href="functions/tie.html">tie()</a></code> using the
<code class="inline"><span class="i">$DB_BTREE</span></code>
 hash bindings as documented in <a href="DB_File.html#In-Memory-Databases">In Memory Databases in DB_File</a>. The <a href="http://search.cpan.org/perldoc/Tie::IxHash">Tie::IxHash</a> module from CPAN might also be
instructive. Although this does keep your hash sorted, you might not
like the slowdown you suffer from the tie interface. Are you sure you
need to do this? :)</p>
<a name="What's-the-difference-between-%22delete%22-and-%22undef%22-with-hashes%3f"></a><h2>What's the difference between "delete" and "undef" with hashes?</h2>
<p>Hashes contain pairs of scalars: the first is the key, the
second is the value. The key will be coerced to a string,
although the value can be any kind of scalar: string,
number, or reference. If a key <code class="inline"><span class="i">$key</span></code>
 is present in
%hash, <code class="inline"><a class="l_k" href="functions/exists.html">exists($hash{$key})</a></code> will return true. The value
for a given key can be <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>, in which case
<code class="inline"><span class="i">$hash</span>{<span class="i">$key</span>}</code>
 will be <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> while <code class="inline"><a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="i">$key</span>}</code>

will return true. This corresponds to (<code class="inline"><span class="i">$key</span></code>
, <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>)
being in the hash.</p>
<p>Pictures help... Here's the <code class="inline"><span class="i">%hash</span></code>
 table:</p>
<pre class="verbatim"><ol><li>      <a class="l_k" href="functions/keys.html">keys</a>  <a class="l_k" href="functions/values.html">values</a></li><li>    +------+------+</li><li>    |  <span class="w">a</span>   |  <span class="n">3</span>   |</li><li>    |  <span class="w">x</span>   |  <span class="n">7</span>   |</li><li>    |  <span class="w">d</span>   |  <span class="n">0</span>   |</li><li>    |  <span class="w">e</span>   |  <span class="n">2</span>   |</li><li>    +------+------+</li></ol></pre><p>And these conditions hold</p>
<pre class="verbatim"><ol><li>    <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}                       <span class="w">is</span> <span class="w">true</span></li><li>    <span class="i">$hash</span>{<span class="q">&#39;d&#39;</span>}                       <span class="w">is</span> <span class="w">false</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$hash</span>{<span class="q">&#39;d&#39;</span>}               <span class="w">is</span> <span class="w">true</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}               <span class="w">is</span> <span class="w">true</span></li><li>    <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}                <span class="w">is</span> <span class="w">true</span> <span class="s">(</span><span class="w">Perl</span> <span class="n">5</span> <span class="w">only</span><span class="s">)</span></li><li>    <a class="l_k" href="functions/grep.html">grep</a> <span class="s">(</span><span class="i">$_</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;a&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span>     <span class="w">is</span> <span class="w">true</span></li></ol></pre><p>If you now say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/undef.html">undef</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}</li></ol></pre><p>your table now reads:</p>
<pre class="verbatim"><ol><li>      <a class="l_k" href="functions/keys.html">keys</a>  <a class="l_k" href="functions/values.html">values</a></li><li>    +------+------+</li><li>    |  <span class="w">a</span>   | <a class="l_k" href="functions/undef.html">undef</a>|</li><li>    |  <span class="w">x</span>   |  <span class="n">7</span>   |</li><li>    |  <span class="w">d</span>   |  <span class="n">0</span>   |</li><li>    |  <span class="w">e</span>   |  <span class="n">2</span>   |</li><li>    +------+------+</li></ol></pre><p>and these conditions now hold; changes in caps:</p>
<pre class="verbatim"><ol><li>    <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}                       <span class="w">is</span> <span class="w">FALSE</span></li><li>    <span class="i">$hash</span>{<span class="q">&#39;d&#39;</span>}                       <span class="w">is</span> <span class="w">false</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$hash</span>{<span class="q">&#39;d&#39;</span>}               <span class="w">is</span> <span class="w">true</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}               <span class="w">is</span> <span class="w">FALSE</span></li><li>    <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}                <span class="w">is</span> <span class="w">true</span> <span class="s">(</span><span class="w">Perl</span> <span class="n">5</span> <span class="w">only</span><span class="s">)</span></li><li>    <a class="l_k" href="functions/grep.html">grep</a> <span class="s">(</span><span class="i">$_</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;a&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span>     <span class="w">is</span> <span class="w">true</span></li></ol></pre><p>Notice the last two: you have an undef value, but a defined key!</p>
<p>Now, consider this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/delete.html">delete</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}</li></ol></pre><p>your table now reads:</p>
<pre class="verbatim"><ol><li>      <a class="l_k" href="functions/keys.html">keys</a>  <a class="l_k" href="functions/values.html">values</a></li><li>    +------+------+</li><li>    |  <span class="w">x</span>   |  <span class="n">7</span>   |</li><li>    |  <span class="w">d</span>   |  <span class="n">0</span>   |</li><li>    |  <span class="w">e</span>   |  <span class="n">2</span>   |</li><li>    +------+------+</li></ol></pre><p>and these conditions now hold; changes in caps:</p>
<pre class="verbatim"><ol><li>    <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}                       <span class="w">is</span> <span class="w">false</span></li><li>    <span class="i">$hash</span>{<span class="q">&#39;d&#39;</span>}                       <span class="w">is</span> <span class="w">false</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$hash</span>{<span class="q">&#39;d&#39;</span>}               <span class="w">is</span> <span class="w">true</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}               <span class="w">is</span> <span class="w">false</span></li><li>    <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="q">&#39;a&#39;</span>}                <span class="w">is</span> <span class="w">FALSE</span> <span class="s">(</span><span class="w">Perl</span> <span class="n">5</span> <span class="w">only</span><span class="s">)</span></li><li>    <a class="l_k" href="functions/grep.html">grep</a> <span class="s">(</span><span class="i">$_</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;a&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span>     <span class="w">is</span> <span class="w">FALSE</span></li></ol></pre><p>See, the whole entry is gone!</p>
<a name="Why-don't-my-tied-hashes-make-the-defined%2fexists-distinction%3f"></a><h2>Why don't my tied hashes make the defined/exists distinction?</h2>
<p>This depends on the tied hash's implementation of EXISTS().
For example, there isn't the concept of undef with hashes
that are tied to DBM* files. It also means that exists() and
defined() do the same thing with a DBM* file, and what they
end up doing is not what they do with ordinary hashes.</p>
<a name="How-do-I-reset-an-each()-operation-part-way-through%3f"></a><h2>How do I reset an each() operation part-way through?</h2>
<p>(contributed by brian d foy)</p>
<p>You can use the <code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code> or <code class="inline"><a class="l_k" href="functions/values.html">values</a></code> functions to reset <code class="inline"><a class="l_k" href="functions/each.html">each</a></code>. To
simply reset the iterator used by <code class="inline"><a class="l_k" href="functions/each.html">each</a></code> without doing anything else,
use one of them in void context:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="sc">;</span> <span class="c"># resets iterator, nothing else.</span></li><li>    <a class="l_k" href="functions/values.html">values</a> <span class="i">%hash</span><span class="sc">;</span> <span class="c"># resets iterator, nothing else.</span></li></ol></pre><p>See the documentation for <code class="inline"><a class="l_k" href="functions/each.html">each</a></code> in <a href="perlfunc.html">perlfunc</a>.</p>
<a name="How-can-I-get-the-unique-keys-from-two-hashes%3f"></a><h2>How can I get the unique keys from two hashes?</h2>
<p>First you extract the keys from the hashes into lists, then solve
the "removing duplicates" problem described above. For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%seen</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> <span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a><span class="s">(</span><span class="i">%foo</span><span class="s">)</span><span class="cm">,</span> <a class="l_k" href="functions/keys.html">keys</a><span class="s">(</span><span class="i">%bar</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$seen</span>{<span class="i">$element</span>}++<span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@uniq</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%seen</span><span class="sc">;</span></li></ol></pre><p>Or more succinctly:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@uniq</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%</span>{<span class="s">{</span><span class="i">%foo</span><span class="cm">,</span><span class="i">%bar</span><span class="s">}</span>}<span class="sc">;</span></li></ol></pre><p>Or if you really want to save space:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%seen</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a> <span class="s">(</span><span class="i">$key</span> = <a class="l_k" href="functions/each.html">each</a> <span class="i">%foo</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$seen</span>{<span class="i">$key</span>}++<span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a> <span class="s">(</span><span class="i">$key</span> = <a class="l_k" href="functions/each.html">each</a> <span class="i">%bar</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$seen</span>{<span class="i">$key</span>}++<span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@uniq</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%seen</span><span class="sc">;</span></li></ol></pre><a name="How-can-I-store-a-multidimensional-array-in-a-DBM-file%3f"></a><h2>How can I store a multidimensional array in a DBM file?</h2>
<p>Either stringify the structure yourself (no fun), or else
get the MLDBM (which uses Data::Dumper) module from CPAN and layer
it on top of either DB_File or GDBM_File. You might also try DBM::Deep, but
it can be a bit slow.</p>
<a name="How-can-I-make-my-hash-remember-the-order-I-put-elements-into-it%3f"></a><h2>How can I make my hash remember the order I put elements into it?</h2>
<p>Use the <a href="http://search.cpan.org/perldoc/Tie::IxHash">Tie::IxHash</a> from CPAN.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Tie::IxHash</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%myhash</span><span class="cm">,</span> <span class="q">&#39;Tie::IxHash&#39;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span>=<span class="n">0</span><span class="sc">;</span> <span class="i">$i</span>&lt;<span class="n">20</span><span class="sc">;</span> <span class="i">$i</span>++<span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$myhash</span>{<span class="i">$i</span>} = <span class="n">2</span>*<span class="i">$i</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@keys</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%myhash</span><span class="sc">;</span></li><li>    <span class="c"># @keys = (0,1,2,3,...)</span></li></ol></pre><a name="Why-does-passing-a-subroutine-an-undefined-element-in-a-hash-create-it%3f"></a><h2>Why does passing a subroutine an undefined element in a hash create it?</h2>
<p>(contributed by brian d foy)</p>
<p>Are you using a really old version of Perl?</p>
<p>Normally, accessing a hash key's value for a nonexistent key will
<i>not</i> create the key.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%hash</span>  = <span class="s">(</span><span class="s">)</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">$hash</span>{ <span class="q">&#39;foo&#39;</span> }<span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This won&#39;t print\n&quot;</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{ <span class="q">&#39;foo&#39;</span> }<span class="sc">;</span></li></ol></pre><p>Passing <code class="inline"><span class="i">$hash</span>{ <span class="q">&#39;foo&#39;</span> }</code>
 to a subroutine used to be a special case, though.
Since you could assign directly to <code class="inline"><span class="i">$_</span>[<span class="n">0</span>]</code>
, Perl had to be ready to
make that assignment so it created the hash key ahead of time:</p>
<pre class="verbatim"><ol><li>    <span class="i">my_sub</span><span class="s">(</span> <span class="i">$hash</span>{ <span class="q">&#39;foo&#39;</span> } <span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This will print before 5.004\n&quot;</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{ <span class="q">&#39;foo&#39;</span> }<span class="sc">;</span></li><li></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>        <span class="c"># $_[0] = &#39;bar&#39;; # create hash key in case you do this</span></li><li>        <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Since Perl 5.004, however, this situation is a special case and Perl
creates the hash key only when you make the assignment:</p>
<pre class="verbatim"><ol><li>    <span class="i">my_sub</span><span class="s">(</span> <span class="i">$hash</span>{ <span class="q">&#39;foo&#39;</span> } <span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This will print, even after 5.004\n&quot;</span> <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{ <span class="q">&#39;foo&#39;</span> }<span class="sc">;</span></li><li></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>        <span class="i">$_</span>[<span class="n">0</span>] = <span class="q">&#39;bar&#39;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>However, if you want the old behavior (and think carefully about that
because it's a weird side effect), you can pass a hash slice instead.
Perl 5.004 didn't make this a special case:</p>
<pre class="verbatim"><ol><li>    <span class="i">my_sub</span><span class="s">(</span> <span class="i">@hash</span>{ <span class="q">qw/foo/</span> } <span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="How-can-I-make-the-Perl-equivalent-of-a-C-structure%2fC%2b%2b-class%2fhash-or-array-of-hashes-or-arrays%3f"></a><h2>How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?</h2>
<p>Usually a hash ref, perhaps like this:</p>
<pre class="verbatim"><ol><li>    <span class="i">$record</span> = <span class="s">{</span></li><li>        <span class="w">NAME</span>   <span class="cm">=&gt;</span> <span class="q">&quot;Jason&quot;</span><span class="cm">,</span></li><li>        <span class="w">EMPNO</span>  <span class="cm">=&gt;</span> <span class="n">132</span><span class="cm">,</span></li><li>        <span class="w">TITLE</span>  <span class="cm">=&gt;</span> <span class="q">&quot;deputy peon&quot;</span><span class="cm">,</span></li><li>        <span class="w">AGE</span>    <span class="cm">=&gt;</span> <span class="n">23</span><span class="cm">,</span></li><li>        <span class="w">SALARY</span> <span class="cm">=&gt;</span> <span class="n">37_000</span><span class="cm">,</span></li><li>        <span class="w">PALS</span>   <span class="cm">=&gt;</span> <span class="s">[</span> <span class="q">&quot;Norbert&quot;</span><span class="cm">,</span> <span class="q">&quot;Rhys&quot;</span><span class="cm">,</span> <span class="q">&quot;Phineas&quot;</span><span class="s">]</span><span class="cm">,</span></li><li>    <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>References are documented in <a href="perlref.html">perlref</a> and <a href="perlreftut.html">perlreftut</a>.
Examples of complex data structures are given in <a href="perldsc.html">perldsc</a> and
<a href="perllol.html">perllol</a>. Examples of structures and object-oriented classes are
in <a href="perlootut.html">perlootut</a>.</p>
<a name="How-can-I-use-a-reference-as-a-hash-key%3f"></a><h2>How can I use a reference as a hash key?</h2>
<p>(contributed by brian d foy and Ben Morrow)</p>
<p>Hash keys are strings, so you can't really use a reference as the key.
When you try to do that, perl turns the reference into its stringified
form (for instance, <code class="inline"><span class="i">HASH</span><span class="s">(</span><span class="n">0xDEADBEEF</span><span class="s">)</span></code>
). From there you can't get
back the reference from the stringified form, at least without doing
some extra work on your own.</p>
<p>Remember that the entry in the hash will still be there even if
the referenced variable  goes out of scope, and that it is entirely
possible for Perl to subsequently allocate a different variable at
the same address. This will mean a new variable might accidentally
be associated with the value for an old.</p>
<p>If you have Perl 5.10 or later, and you just want to store a value
against the reference for lookup later, you can use the core
Hash::Util::Fieldhash module. This will also handle renaming the
keys if you use multiple threads (which causes all variables to be
reallocated at new addresses, changing their stringification), and
garbage-collecting the entries when the referenced variable goes out
of scope.</p>
<p>If you actually need to be able to get a real reference back from
each hash entry, you can use the Tie::RefHash module, which does the
required work for you.</p>
<a name="How-can-I-check-if-a-key-exists-in-a-multilevel-hash%3f"></a><h2>How can I check if a key exists in a multilevel hash?</h2>
<p>(contributed by brian d foy)</p>
<p>The trick to this problem is avoiding accidental autovivification. If
you want to check three keys deep, you might na&#xef;vely try this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%hash</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="w">key1</span>}{<span class="w">key2</span>}{<span class="w">key3</span>} <span class="s">)</span> <span class="s">{</span></li><li>        ...<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Even though you started with a completely empty hash, after that call to
<code class="inline"><a class="l_k" href="functions/exists.html">exists</a></code> you've created the structure you needed to check for <code class="inline"><span class="w">key3</span></code>
:</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <span class="s">(</span></li><li>              <span class="q">&#39;key1&#39;</span> <span class="cm">=&gt;</span> <span class="s">{</span></li><li>                          <span class="q">&#39;key2&#39;</span> <span class="cm">=&gt;</span> <span class="s">{</span><span class="s">}</span></li><li>                        <span class="s">}</span></li><li>            <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>That's autovivification. You can get around this in a few ways. The
easiest way is to just turn it off. The lexical <code class="inline"><span class="w">autovivification</span></code>

pragma is available on CPAN. Now you don't add to the hash:</p>
<pre class="verbatim"><ol><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/no.html">no</a> <span class="w">autovivification</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">%hash</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="w">key1</span>}{<span class="w">key2</span>}{<span class="w">key3</span>} <span class="s">)</span> <span class="s">{</span></li><li>            ...<span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>The <a href="http://search.cpan.org/perldoc/Data::Diver">Data::Diver</a> module on CPAN can do it for you too. Its <code class="inline"><span class="w">Dive</span></code>

subroutine can tell you not only if the keys exist but also get the
value:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Data::Diver</span> <span class="q">qw(Dive)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@exists</span> = <span class="i">Dive</span><span class="s">(</span> \<span class="i">%hash</span><span class="cm">,</span> <span class="q">qw(key1 key2 key3)</span> <span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span>  ! <span class="i">@exists</span>  <span class="s">)</span> <span class="s">{</span></li><li>        ...<span class="sc">;</span> <span class="c"># keys do not exist</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/elsif.html">elsif</a><span class="s">(</span>  ! <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$exists</span>[<span class="n">0</span>]  <span class="s">)</span> <span class="s">{</span></li><li>        ...<span class="sc">;</span> <span class="c"># keys exist but value is undef</span></li><li>    <span class="s">}</span></li></ol></pre><p>You can easily do this yourself too by checking each level of the hash
before you move onto the next level. This is essentially what
<a href="http://search.cpan.org/perldoc/Data::Diver">Data::Diver</a> does for you:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a><span class="s">(</span> <span class="i">check_hash</span><span class="s">(</span> \<span class="i">%hash</span><span class="cm">,</span> <span class="q">qw(key1 key2 key3)</span> <span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>        ...<span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="check_hash"></a>    sub <span class="m">check_hash</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/my.html">my</a><span class="s">(</span> <span class="i">$hash</span><span class="cm">,</span> <span class="i">@keys</span> <span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li></li><li>       <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">@keys</span><span class="sc">;</span></li><li></li><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="i">@keys</span> <span class="s">)</span> <span class="s">{</span></li><li>           <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$hash</span>-&gt;{<span class="i">$key</span>} <span class="s">}</span><span class="sc">;</span></li><li>           <span class="i">$hash</span> = <span class="i">$hash</span>-&gt;{<span class="i">$key</span>}<span class="sc">;</span></li><li>        <span class="s">}</span></li><li></li><li>       <a class="l_k" href="functions/return.html">return</a> <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><a name="How-can-I-prevent-addition-of-unwanted-keys-into-a-hash%3f"></a><h2>How can I prevent addition of unwanted keys into a hash?</h2>
<p>Since version 5.8.0, hashes can be <i>restricted</i> to a fixed number
of given keys. Methods for creating and dealing with restricted hashes
are exported by the <a href="Hash/Util.html">Hash::Util</a> module.</p>
<a name="Data%3a-Misc"></a><h1>Data: Misc</h1>
<a name="How-do-I-handle-binary-data-correctly%3f"></a><h2>How do I handle binary data correctly?</h2>
<p>Perl is binary-clean, so it can handle binary data just fine.
On Windows or DOS, however, you have to use <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> for binary
files to avoid conversions for line endings. In general, you should
use <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> any time you want to work with binary data.</p>
<p>Also see <a href="functions/binmode.html">binmode</a> or <a href="perlopentut.html">perlopentut</a>.</p>
<p>If you're concerned about 8-bit textual data then see <a href="perllocale.html">perllocale</a>.
If you want to deal with multibyte characters, however, there are
some gotchas. See the section on Regular Expressions.</p>
<a name="How-do-I-determine-whether-a-scalar-is-a-number%2fwhole%2finteger%2ffloat%3f"></a><h2>How do I determine whether a scalar is a number/whole/integer/float?</h2>
<p>Assuming that you don't care about IEEE notations like "NaN" or
"Infinity", you probably just want to use a regular expression (see also
<a href="perlretut.html">perlretut</a> and <a href="perlre.html">perlre</a>):</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="n">5.010</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="q">/\D/</span> <span class="s">)</span></li><li>        <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;\thas nondigits&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="q">/^\d+\z/</span> <span class="s">)</span></li><li>        <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;\tis a whole number&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="q">/^-?\d+\z/</span> <span class="s">)</span></li><li>        <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;\tis an integer&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="q">/^[+-]?\d+\z/</span> <span class="s">)</span></li><li>        <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;\tis a +/- integer&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="q">/^-?(?:\d+\.?|\.\d)\d*\z/</span> <span class="s">)</span></li><li>        <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;\tis a real number&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="q">/^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i</span> <span class="s">)</span></li><li>        <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;\tis a C float&quot;</span> <span class="s">}</span></li></ol></pre><p>There are also some commonly used modules for the task.
<a href="Scalar/Util.html">Scalar::Util</a> (distributed with 5.8) provides access to perl's
internal function <code class="inline"><span class="w">looks_like_number</span></code>
 for determining whether a
variable looks like a number. <a href="http://search.cpan.org/perldoc/Data::Types">Data::Types</a> exports functions that
validate data types using both the above and other regular
expressions. Thirdly, there is <a href="http://search.cpan.org/perldoc/Regexp::Common">Regexp::Common</a> which has regular
expressions to match various types of numbers. Those three modules are
available from the CPAN.</p>
<p>If you're on a POSIX system, Perl supports the <code class="inline"><span class="w">POSIX::strtod</span></code>

function for converting strings to doubles (and also <code class="inline"><span class="w">POSIX::strtol</span></code>

for longs). Its semantics are somewhat cumbersome, so here's a
<code class="inline"><span class="w">getnum</span></code>
 wrapper function for more convenient access. This function
takes a string and returns the number it found, or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> for input
that isn't a C float. The <code class="inline"><span class="w">is_numeric</span></code>
 function is a front end to
<code class="inline"><span class="w">getnum</span></code>
 if you just want to say, "Is this a float?"</p>
<pre class="verbatim"><ol><li><a name="getnum"></a>    sub <span class="m">getnum</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw(strtod)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <span class="i">$str</span> =~ <span class="q">s/^\s+//</span><span class="sc">;</span></li><li>        <span class="i">$str</span> =~ <span class="q">s/\s+$//</span><span class="sc">;</span></li><li>        <span class="i">$!</span> = <span class="n">0</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$num</span><span class="cm">,</span> <span class="i">$unparsed</span><span class="s">)</span> = <span class="i">strtod</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="s">(</span><span class="i">$str</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;&#39;</span><span class="s">)</span> || <span class="s">(</span><span class="i">$unparsed</span> != <span class="n">0</span><span class="s">)</span> || <span class="i">$!</span><span class="s">)</span> <span class="s">{</span></li><li>                <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>            <a class="l_k" href="functions/return.html">return</a> <span class="i">$num</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li><li></li><li><a name="is_numeric"></a>    sub <span class="m">is_numeric</span> <span class="s">{</span> <a class="l_k" href="functions/defined.html">defined</a> <span class="i">getnum</span><span class="s">(</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span> <span class="s">}</span></li></ol></pre><p>Or you could check out the <a href="http://search.cpan.org/perldoc/String::Scanf">String::Scanf</a> module on the CPAN
instead.</p>
<a name="How-do-I-keep-persistent-data-across-program-calls%3f"></a><h2>How do I keep persistent data across program calls?</h2>
<p>For some specific applications, you can use one of the DBM modules.
See <a href="AnyDBM_File.html">AnyDBM_File</a>. More generically, you should consult the <a href="http://search.cpan.org/perldoc/FreezeThaw">FreezeThaw</a>
or <a href="Storable.html">Storable</a> modules from CPAN. Starting from Perl 5.8, <a href="Storable.html">Storable</a> is part
of the standard distribution. Here's one example using <a href="Storable.html">Storable</a>'s <code class="inline"><span class="w">store</span></code>

and <code class="inline"><span class="w">retrieve</span></code>
 functions:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Storable</span><span class="sc">;</span></li><li>    <span class="i">store</span><span class="s">(</span>\<span class="i">%hash</span><span class="cm">,</span> <span class="q">&quot;filename&quot;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># later on...</span></li><li>    <span class="i">$href</span> = <span class="i">retrieve</span><span class="s">(</span><span class="q">&quot;filename&quot;</span><span class="s">)</span><span class="sc">;</span>        <span class="c"># by ref</span></li><li>    <span class="i">%hash</span> = <span class="i">%</span>{ <span class="i">retrieve</span><span class="s">(</span><span class="q">&quot;filename&quot;</span><span class="s">)</span> }<span class="sc">;</span>   <span class="c"># direct to hash</span></li></ol></pre><a name="How-do-I-print-out-or-copy-a-recursive-data-structure%3f"></a><h2>How do I print out or copy a recursive data structure?</h2>
<p>The <a href="Data/Dumper.html">Data::Dumper</a> module on CPAN (or the 5.005 release of Perl) is great
for printing out data structures. The <a href="Storable.html">Storable</a> module on CPAN (or the
5.8 release of Perl), provides a function called <code class="inline"><span class="w">dclone</span></code>
 that recursively
copies its argument.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Storable</span> <span class="q">qw(dclone)</span><span class="sc">;</span></li><li>    <span class="i">$r2</span> = <span class="i">dclone</span><span class="s">(</span><span class="i">$r1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Where <code class="inline"><span class="i">$r1</span></code>
 can be a reference to any kind of data structure you'd like.
It will be deeply copied. Because <code class="inline"><span class="w">dclone</span></code>
 takes and returns references,
you'd have to add extra punctuation if you had a hash of arrays that
you wanted to copy.</p>
<pre class="verbatim"><ol><li>    <span class="i">%newhash</span> = <span class="i">%</span>{ <span class="i">dclone</span><span class="s">(</span>\<span class="i">%oldhash</span><span class="s">)</span> }<span class="sc">;</span></li></ol></pre><a name="How-do-I-define-methods-for-every-class%2fobject%3f"></a><h2>How do I define methods for every class/object?</h2>
<p>(contributed by Ben Morrow)</p>
<p>You can use the <code class="inline"><span class="w">UNIVERSAL</span></code>
 class (see <a href="UNIVERSAL.html">UNIVERSAL</a>). However, please
be very careful to consider the consequences of doing this: adding
methods to every object is very likely to have unintended
consequences. If possible, it would be better to have all your object
inherit from some common base class, or to use an object system like
Moose that supports roles.</p>
<a name="How-do-I-verify-a-credit-card-checksum%3f"></a><h2>How do I verify a credit card checksum?</h2>
<p>Get the <a href="http://search.cpan.org/perldoc/Business::CreditCard">Business::CreditCard</a> module from CPAN.</p>
<a name="How-do-I-pack-arrays-of-doubles-or-floats-for-XS-code%3f"></a><h2>How do I pack arrays of doubles or floats for XS code?</h2>
<p>The arrays.h/arrays.c code in the <a href="http://search.cpan.org/perldoc/PGPLOT">PGPLOT</a> module on CPAN does just this.
If you're doing a lot of float or double processing, consider using
the <a href="http://search.cpan.org/perldoc/PDL">PDL</a> module from CPAN instead--it makes number-crunching easy.</p>
<p>See <a href="http://search.cpan.org/dist/PGPLOT">http://search.cpan.org/dist/PGPLOT</a> for the code.</p>
<a name="AUTHOR-AND-COPYRIGHT"></a><h1>AUTHOR AND COPYRIGHT</h1>
<p>Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and
other authors as noted. All rights reserved.</p>
<p>This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Irrespective of its distribution, all code examples in this file
are hereby placed into the public domain. You are permitted and
encouraged to use this code in your own programs for fun
or for profit as you see fit. A simple comment in the code giving
credit would be courteous but is not required.</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="#VERSION">VERSION</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Data%3a-Numbers">Data: Numbers</a><ul><li><a href="#Why-am-I-getting-long-decimals-(eg%2c-19.9499999999999)-instead-of-the-numbers-I-should-be-getting-(eg%2c-19.95)%3f">Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?</a><li><a href="#Why-is-int()-broken%3f">Why is int() broken?</a><li><a href="#Why-isn't-my-octal-data-interpreted-correctly%3f">Why isn't my octal data interpreted correctly?</a><li><a href="#Does-Perl-have-a-round()-function%3f-What-about-ceil()-and-floor()%3f-Trig-functions%3f">Does Perl have a round() function? What about ceil() and floor()? Trig functions?</a><li><a href="#How-do-I-convert-between-numeric-representations%2fbases%2fradixes%3f">How do I convert between numeric representations/bases/radixes?</a><li><a href="#Why-doesn't-%26-work-the-way-I-want-it-to%3f">Why doesn't & work the way I want it to?</a><li><a href="#How-do-I-multiply-matrices%3f">How do I multiply matrices?</a><li><a href="#How-do-I-perform-an-operation-on-a-series-of-integers%3f">How do I perform an operation on a series of integers?</a><li><a href="#How-can-I-output-Roman-numerals%3f">How can I output Roman numerals?</a><li><a href="#Why-aren't-my-random-numbers-random%3f">Why aren't my random numbers random?</a><li><a href="#How-do-I-get-a-random-number-between-X-and-Y%3f">How do I get a random number between X and Y?</a></ul><li><a href="#Data%3a-Dates">Data: Dates</a><ul><li><a href="#How-do-I-find-the-day-or-week-of-the-year%3f">How do I find the day or week of the year?</a><li><a href="#How-do-I-find-the-current-century-or-millennium%3f">How do I find the current century or millennium?</a><li><a href="#How-can-I-compare-two-dates-and-find-the-difference%3f">How can I compare two dates and find the difference?</a><li><a href="#How-can-I-take-a-string-and-turn-it-into-epoch-seconds%3f">How can I take a string and turn it into epoch seconds?</a><li><a href="#How-can-I-find-the-Julian-Day%3f">How can I find the Julian Day?</a><li><a href="#How-do-I-find-yesterday's-date%3f">How do I find yesterday's date?
    
   
</a><li><a href="#Does-Perl-have-a-Year-2000-or-2038-problem%3f-Is-Perl-Y2K-compliant%3f">Does Perl have a Year 2000 or 2038 problem? Is Perl Y2K compliant?</a></ul><li><a href="#Data%3a-Strings">Data: Strings</a><ul><li><a href="#How-do-I-validate-input%3f">How do I validate input?</a><li><a href="#How-do-I-unescape-a-string%3f">How do I unescape a string?</a><li><a href="#How-do-I-remove-consecutive-pairs-of-characters%3f">How do I remove consecutive pairs of characters?</a><li><a href="#How-do-I-expand-function-calls-in-a-string%3f">How do I expand function calls in a string?</a><li><a href="#How-do-I-find-matching%2fnesting-anything%3f">How do I find matching/nesting anything?</a><li><a href="#How-do-I-reverse-a-string%3f">How do I reverse a string?</a><li><a href="#How-do-I-expand-tabs-in-a-string%3f">How do I expand tabs in a string?</a><li><a href="#How-do-I-reformat-a-paragraph%3f">How do I reformat a paragraph?</a><li><a href="#How-can-I-access-or-change-N-characters-of-a-string%3f">How can I access or change N characters of a string?</a><li><a href="#How-do-I-change-the-Nth-occurrence-of-something%3f">How do I change the Nth occurrence of something?</a><li><a href="#How-can-I-count-the-number-of-occurrences-of-a-substring-within-a-string%3f">How can I count the number of occurrences of a substring within a string?</a><li><a href="#How-do-I-capitalize-all-the-words-on-one-line%3f">How do I capitalize all the words on one line?
   </a><li><a href="#How-can-I-split-a-%5bcharacter%5d-delimited-string-except-when-inside-%5bcharacter%5d%3f">How can I split a [character]-delimited string except when inside [character]?</a><li><a href="#How-do-I-strip-blank-space-from-the-beginning%2fend-of-a-string%3f">How do I strip blank space from the beginning/end of a string?</a><li><a href="#How-do-I-pad-a-string-with-blanks-or-pad-a-number-with-zeroes%3f">How do I pad a string with blanks or pad a number with zeroes?</a><li><a href="#How-do-I-extract-selected-columns-from-a-string%3f">How do I extract selected columns from a string?</a><li><a href="#How-do-I-find-the-soundex-value-of-a-string%3f">How do I find the soundex value of a string?</a><li><a href="#How-can-I-expand-variables-in-text-strings%3f">How can I expand variables in text strings?</a><li><a href="#What's-wrong-with-always-quoting-%22%24vars%22%3f">What's wrong with always quoting "$vars"?</a><li><a href="#Why-don't-my-%3c%3cHERE-documents-work%3f">Why don't my <<HERE documents work?</a></ul><li><a href="#Data%3a-Arrays">Data: Arrays</a><ul><li><a href="#What-is-the-difference-between-a-list-and-an-array%3f">What is the difference between a list and an array?</a><li><a href="#What-is-the-difference-between-%24array%5b1%5d-and-%40array%5b1%5d%3f">What is the difference between $array[1] and @array[1]?</a><li><a href="#How-can-I-remove-duplicate-elements-from-a-list-or-array%3f">How can I remove duplicate elements from a list or array?</a><li><a href="#How-can-I-tell-whether-a-certain-element-is-contained-in-a-list-or-array%3f">How can I tell whether a certain element is contained in a list or array?</a><li><a href="#How-do-I-compute-the-difference-of-two-arrays%3f-How-do-I-compute-the-intersection-of-two-arrays%3f">How do I compute the difference of two arrays? How do I compute the intersection of two arrays?</a><li><a href="#How-do-I-test-whether-two-arrays-or-hashes-are-equal%3f">How do I test whether two arrays or hashes are equal?</a><li><a href="#How-do-I-find-the-first-array-element-for-which-a-condition-is-true%3f">How do I find the first array element for which a condition is true?</a><li><a href="#How-do-I-handle-linked-lists%3f">How do I handle linked lists?</a><li><a href="#How-do-I-handle-circular-lists%3f">How do I handle circular lists?
   
 </a><li><a href="#How-do-I-shuffle-an-array-randomly%3f">How do I shuffle an array randomly?</a><li><a href="#How-do-I-process%2fmodify-each-element-of-an-array%3f">How do I process/modify each element of an array?</a><li><a href="#How-do-I-select-a-random-element-from-an-array%3f">How do I select a random element from an array?</a><li><a href="#How-do-I-permute-N-elements-of-a-list%3f">How do I permute N elements of a list?
   
 </a><li><a href="#How-do-I-sort-an-array-by-(anything)%3f">How do I sort an array by (anything)?</a><li><a href="#How-do-I-manipulate-arrays-of-bits%3f">How do I manipulate arrays of bits?</a><li><a href="#Why-does-defined()-return-true-on-empty-arrays-and-hashes%3f">Why does defined() return true on empty arrays and hashes?</a></ul><li><a href="#Data%3a-Hashes-(Associative-Arrays)">Data: Hashes (Associative Arrays)</a><ul><li><a href="#How-do-I-process-an-entire-hash%3f">How do I process an entire hash?</a><li><a href="#How-do-I-merge-two-hashes%3f">How do I merge two hashes?
  </a><li><a href="#What-happens-if-I-add-or-remove-keys-from-a-hash-while-iterating-over-it%3f">What happens if I add or remove keys from a hash while iterating over it?</a><li><a href="#How-do-I-look-up-a-hash-element-by-value%3f">How do I look up a hash element by value?</a><li><a href="#How-can-I-know-how-many-entries-are-in-a-hash%3f">How can I know how many entries are in a hash?</a><li><a href="#How-do-I-sort-a-hash-(optionally-by-value-instead-of-key)%3f">How do I sort a hash (optionally by value instead of key)?</a><li><a href="#How-can-I-always-keep-my-hash-sorted%3f">How can I always keep my hash sorted?
</a><li><a href="#What's-the-difference-between-%22delete%22-and-%22undef%22-with-hashes%3f">What's the difference between "delete" and "undef" with hashes?</a><li><a href="#Why-don't-my-tied-hashes-make-the-defined%2fexists-distinction%3f">Why don't my tied hashes make the defined/exists distinction?</a><li><a href="#How-do-I-reset-an-each()-operation-part-way-through%3f">How do I reset an each() operation part-way through?</a><li><a href="#How-can-I-get-the-unique-keys-from-two-hashes%3f">How can I get the unique keys from two hashes?</a><li><a href="#How-can-I-store-a-multidimensional-array-in-a-DBM-file%3f">How can I store a multidimensional array in a DBM file?</a><li><a href="#How-can-I-make-my-hash-remember-the-order-I-put-elements-into-it%3f">How can I make my hash remember the order I put elements into it?</a><li><a href="#Why-does-passing-a-subroutine-an-undefined-element-in-a-hash-create-it%3f">Why does passing a subroutine an undefined element in a hash create it?</a><li><a href="#How-can-I-make-the-Perl-equivalent-of-a-C-structure%2fC%2b%2b-class%2fhash-or-array-of-hashes-or-arrays%3f">How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?</a><li><a href="#How-can-I-use-a-reference-as-a-hash-key%3f">How can I use a reference as a hash key?</a><li><a href="#How-can-I-check-if-a-key-exists-in-a-multilevel-hash%3f">How can I check if a key exists in a multilevel hash?</a><li><a href="#How-can-I-prevent-addition-of-unwanted-keys-into-a-hash%3f">How can I prevent addition of unwanted keys into a hash?</a></ul><li><a href="#Data%3a-Misc">Data: Misc</a><ul><li><a href="#How-do-I-handle-binary-data-correctly%3f">How do I handle binary data correctly?</a><li><a href="#How-do-I-determine-whether-a-scalar-is-a-number%2fwhole%2finteger%2ffloat%3f">How do I determine whether a scalar is a number/whole/integer/float?</a><li><a href="#How-do-I-keep-persistent-data-across-program-calls%3f">How do I keep persistent data across program calls?</a><li><a href="#How-do-I-print-out-or-copy-a-recursive-data-structure%3f">How do I print out or copy a recursive data structure?</a><li><a href="#How-do-I-define-methods-for-every-class%2fobject%3f">How do I define methods for every class/object?</a><li><a href="#How-do-I-verify-a-credit-card-checksum%3f">How do I verify a credit card checksum?</a><li><a href="#How-do-I-pack-arrays-of-doubles-or-floats-for-XS-code%3f">How do I pack arrays of doubles or floats for XS code?</a></ul><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND 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(0);
  perldoc.pageName    = 'perlfaq4';
  perldoc.pageAddress = 'perlfaq4.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>