1=head1 NAME 2 3perlfaq4 - Data Manipulation 4 5=head1 VERSION 6 7version 5.20240218 8 9=head1 DESCRIPTION 10 11This section of the FAQ answers questions related to manipulating 12numbers, dates, strings, arrays, hashes, and miscellaneous data issues. 13 14=head1 Data: Numbers 15 16=head2 Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? 17 18For the long explanation, see David Goldberg's "What Every Computer 19Scientist Should Know About Floating-Point Arithmetic" 20(L<http://web.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf>). 21 22Internally, your computer represents floating-point numbers in binary. 23Digital (as in powers of two) computers cannot store all numbers 24exactly. Some real numbers lose precision in the process. This is a 25problem with how computers store numbers and affects all computer 26languages, not just Perl. 27 28L<perlnumber> shows the gory details of number representations and 29conversions. 30 31To limit the number of decimal places in your numbers, you can use the 32C<printf> or C<sprintf> function. See 33L<perlop/"Floating-point Arithmetic"> for more details. 34 35 printf "%.2f", 10/3; 36 37 my $number = sprintf "%.2f", 10/3; 38 39=head2 Why is int() broken? 40 41Your C<int()> is most probably working just fine. It's the numbers that 42aren't quite what you think. 43 44First, see the answer to "Why am I getting long decimals 45(eg, 19.9499999999999) instead of the numbers I should be getting 46(eg, 19.95)?". 47 48For example, this 49 50 print int(0.6/0.2-2), "\n"; 51 52will in most computers print 0, not 1, because even such simple 53numbers as 0.6 and 0.2 cannot be presented exactly by floating-point 54numbers. What you think in the above as 'three' is really more like 552.9999999999999995559. 56 57=head2 Why isn't my octal data interpreted correctly? 58 59(contributed by brian d foy) 60 61You're probably trying to convert a string to a number, which Perl only 62converts as a decimal number. When Perl converts a string to a number, it 63ignores leading spaces and zeroes, then assumes the rest of the digits 64are in base 10: 65 66 my $string = '0644'; 67 68 print $string + 0; # prints 644 69 70 print $string + 44; # prints 688, certainly not octal! 71 72This problem usually involves one of the Perl built-ins that has the 73same name a Unix command that uses octal numbers as arguments on the 74command line. In this example, C<chmod> on the command line knows that 75its first argument is octal because that's what it does: 76 77 %prompt> chmod 644 file 78 79If you want to use the same literal digits (644) in Perl, you have to tell 80Perl to treat them as octal numbers either by prefixing the digits with 81a C<0> or using C<oct>: 82 83 chmod( 0644, $filename ); # right, has leading zero 84 chmod( oct(644), $filename ); # also correct 85 86The problem comes in when you take your numbers from something that Perl 87thinks is a string, such as a command line argument in C<@ARGV>: 88 89 chmod( $ARGV[0], $filename ); # wrong, even if "0644" 90 91 chmod( oct($ARGV[0]), $filename ); # correct, treat string as octal 92 93You can always check the value you're using by printing it in octal 94notation to ensure it matches what you think it should be. Print it 95in octal and decimal format: 96 97 printf "0%o %d", $number, $number; 98 99=head2 Does Perl have a round() function? What about ceil() and floor()? Trig functions? 100 101Remember that C<int()> merely truncates toward 0. For rounding to a 102certain number of digits, C<sprintf()> or C<printf()> is usually the 103easiest route. 104 105 printf("%.3f", 3.1415926535); # prints 3.142 106 107The L<POSIX> module (part of the standard Perl distribution) 108implements C<ceil()>, C<floor()>, and a number of other mathematical 109and trigonometric functions. 110 111 use POSIX; 112 my $ceil = ceil(3.5); # 4 113 my $floor = floor(3.5); # 3 114 115In 5.000 to 5.003 perls, trigonometry was done in the L<Math::Complex> 116module. With 5.004, the L<Math::Trig> module (part of the standard Perl 117distribution) implements the trigonometric functions. Internally it 118uses the L<Math::Complex> module and some functions can break out from 119the real axis into the complex plane, for example the inverse sine of 1202. 121 122Rounding in financial applications can have serious implications, and 123the rounding method used should be specified precisely. In these 124cases, it probably pays not to trust whichever system of rounding is 125being used by Perl, but instead to implement the rounding function you 126need yourself. 127 128To see why, notice how you'll still have an issue on half-way-point 129alternation: 130 131 for (my $i = -5; $i <= 5; $i += 0.5) { printf "%.0f ",$i } 132 133 -5 -4 -4 -4 -3 -2 -2 -2 -1 -0 0 0 1 2 2 2 3 4 4 4 5 134 135Don't blame Perl. It's the same as in C. IEEE says we have to do 136this. Perl numbers whose absolute values are integers under 2**31 (on 13732-bit machines) will work pretty much like mathematical integers. 138Other numbers are not guaranteed. 139 140=head2 How do I convert between numeric representations/bases/radixes? 141 142As always with Perl there is more than one way to do it. Below are a 143few examples of approaches to making common conversions between number 144representations. This is intended to be representational rather than 145exhaustive. 146 147Some of the examples later in L<perlfaq4> use the L<Bit::Vector> 148module from CPAN. The reason you might choose L<Bit::Vector> over the 149perl built-in functions is that it works with numbers of ANY size, 150that it is optimized for speed on some operations, and for at least 151some programmers the notation might be familiar. 152 153=over 4 154 155=item How do I convert hexadecimal into decimal 156 157Using perl's built in conversion of C<0x> notation: 158 159 my $dec = 0xDEADBEEF; 160 161Using the C<hex> function: 162 163 my $dec = hex("DEADBEEF"); 164 165Using C<pack>: 166 167 my $dec = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8))); 168 169Using the CPAN module C<Bit::Vector>: 170 171 use Bit::Vector; 172 my $vec = Bit::Vector->new_Hex(32, "DEADBEEF"); 173 my $dec = $vec->to_Dec(); 174 175=item How do I convert from decimal to hexadecimal 176 177Using C<sprintf>: 178 179 my $hex = sprintf("%X", 3735928559); # upper case A-F 180 my $hex = sprintf("%x", 3735928559); # lower case a-f 181 182Using C<unpack>: 183 184 my $hex = unpack("H*", pack("N", 3735928559)); 185 186Using L<Bit::Vector>: 187 188 use Bit::Vector; 189 my $vec = Bit::Vector->new_Dec(32, -559038737); 190 my $hex = $vec->to_Hex(); 191 192And L<Bit::Vector> supports odd bit counts: 193 194 use Bit::Vector; 195 my $vec = Bit::Vector->new_Dec(33, 3735928559); 196 $vec->Resize(32); # suppress leading 0 if unwanted 197 my $hex = $vec->to_Hex(); 198 199=item How do I convert from octal to decimal 200 201Using Perl's built in conversion of numbers with leading zeros: 202 203 my $dec = 033653337357; # note the leading 0! 204 205Using the C<oct> function: 206 207 my $dec = oct("33653337357"); 208 209Using L<Bit::Vector>: 210 211 use Bit::Vector; 212 my $vec = Bit::Vector->new(32); 213 $vec->Chunk_List_Store(3, split(//, reverse "33653337357")); 214 my $dec = $vec->to_Dec(); 215 216=item How do I convert from decimal to octal 217 218Using C<sprintf>: 219 220 my $oct = sprintf("%o", 3735928559); 221 222Using L<Bit::Vector>: 223 224 use Bit::Vector; 225 my $vec = Bit::Vector->new_Dec(32, -559038737); 226 my $oct = reverse join('', $vec->Chunk_List_Read(3)); 227 228=item How do I convert from binary to decimal 229 230Perl 5.6 lets you write binary numbers directly with 231the C<0b> notation: 232 233 my $number = 0b10110110; 234 235Using C<oct>: 236 237 my $input = "10110110"; 238 my $decimal = oct( "0b$input" ); 239 240Using C<pack> and C<ord>: 241 242 my $decimal = ord(pack('B8', '10110110')); 243 244Using C<pack> and C<unpack> for larger strings: 245 246 my $int = unpack("N", pack("B32", 247 substr("0" x 32 . "11110101011011011111011101111", -32))); 248 my $dec = sprintf("%d", $int); 249 250 # substr() is used to left-pad a 32-character string with zeros. 251 252Using L<Bit::Vector>: 253 254 my $vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111"); 255 my $dec = $vec->to_Dec(); 256 257=item How do I convert from decimal to binary 258 259Using C<sprintf> (perl 5.6+): 260 261 my $bin = sprintf("%b", 3735928559); 262 263Using C<unpack>: 264 265 my $bin = unpack("B*", pack("N", 3735928559)); 266 267Using L<Bit::Vector>: 268 269 use Bit::Vector; 270 my $vec = Bit::Vector->new_Dec(32, -559038737); 271 my $bin = $vec->to_Bin(); 272 273The remaining transformations (e.g. hex -> oct, bin -> hex, etc.) 274are left as an exercise to the inclined reader. 275 276=back 277 278=head2 Why doesn't & work the way I want it to? 279 280Perl's C<&> bitwise operator works on both numbers and strings, 281sometimes producing surprising results when you expected a number 282but received a string. You probably expected perl to automatically 283convert the operands to numbers like the mathematical operators would. 284Instead, perl treats string operands as bitvectors. 285 286Consider the bitwise difference between the number 3 and the bitvector 287represented by "3". A number has the bit pattern for its magnitude. The 288number 3 is 0b11 (a 2 and a 1). The bitvector has the bit pattern that 289is the ordinal value for each octet, and that value is unrelated to any 290numeric value that the digit represents. The character "3" is the 291bitvector 0b0011_0011. 292 293These operations have different results even though you might think they 294look like the same "number": 295 296 11 & 3; # 0b0000_1011 & 0b0000_0011 297 # -> 0b0000_0011 (number 3) 298 "11" & "3"; # 0b0011_0001_0011_0001 & 0b0011_0011 299 # -> 0b0011_0001 (ASCII char "1") 300 301Note that if any operand has a numeric value, perl uses numeric 302semantics (although you should not count on this): 303 304 my($i, $j) = ( 11, 3 ); # $i & $j # 11 & 3 -> 3 305 my($i, $j) = ("11", 3 ); # $i & $j # 11 & 3 -> 3 306 my($i, $j) = ("11", "3"); # $i & $j # "11" & "3" -> 1 307 308Remember that a perl scalar can have both string and numeric values at 309the same time. A value that starts as a string and has never encountered 310a numeric operation has no numeric value yet. Perl does this to save 311time and work so it doesn't have to decide a numeric value for a scalar 312it might never use as a number. In that case, string semantics wins. But, 313if there is a numeric value already, numeric semantics win. Force perl 314to compute the numeric value by adding 0: 315 316 my($i, $j) = ("11", "3"); $j += 0 # $i & $j # "11" & 3 -> 3 317 318However, this is not a documented feature, or as L<perlop> says, it "is not 319well defined". One way to fix ensure numeric semantics is to explicitly 320convert both of values to numbers: 321 322 (0+$i) & (0+$j) 323 324To fix this annoyance, Perl v5.22 separated the string and number 325behavior. The C<bitwise> feature introduced four new operators that 326would work with only string semantics: C<&.>, C<|.>, C<^.>, and C<~.>. 327The original operators, C<&>, C<|>, C<^>, and C<~>, would then apply 328only numeric semantics. 329 330Enable this feature explicitly with L<feature>: 331 332 use feature qw(bitwise); 333 334Or, as of v5.28, require the minimum version of perl with C<use>: 335 336 use v5.28; # bitwise feature for free 337 338=head2 How do I multiply matrices? 339 340Use the L<Math::Matrix> or L<Math::MatrixReal> modules (available from CPAN) 341or the L<PDL> extension (also available from CPAN). 342 343=head2 How do I perform an operation on a series of integers? 344 345To call a function on each element in an array, and collect the 346results, use: 347 348 my @results = map { my_func($_) } @array; 349 350For example: 351 352 my @triple = map { 3 * $_ } @single; 353 354To call a function on each element of an array, but ignore the 355results: 356 357 foreach my $iterator (@array) { 358 some_func($iterator); 359 } 360 361To call a function on each integer in a (small) range, you B<can> use: 362 363 my @results = map { some_func($_) } (5 .. 25); 364 365but you should be aware that in this form, the C<..> operator 366creates a list of all integers in the range, which can take a lot of 367memory for large ranges. However, the problem does not occur when 368using C<..> within a C<for> loop, because in that case the range 369operator is optimized to I<iterate> over the range, without creating 370the entire list. So 371 372 my @results = (); 373 for my $i (5 .. 500_005) { 374 push(@results, some_func($i)); 375 } 376 377or even 378 379 push(@results, some_func($_)) for 5 .. 500_005; 380 381will not create an intermediate list of 500,000 integers. 382 383=head2 How can I output Roman numerals? 384 385Get the L<http://www.cpan.org/modules/by-module/Roman> module. 386 387=head2 Why aren't my random numbers random? 388 389If you're using a version of Perl before 5.004, you must call C<srand> 390once at the start of your program to seed the random number generator. 391 392 BEGIN { srand() if $] < 5.004 } 393 3945.004 and later automatically call C<srand> at the beginning. Don't 395call C<srand> more than once--you make your numbers less random, 396rather than more. 397 398Computers are good at being predictable and bad at being random 399(despite appearances caused by bugs in your programs :-). The 400F<random> article in the "Far More Than You Ever Wanted To Know" 401collection in L<http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz>, courtesy 402of Tom Phoenix, talks more about this. John von Neumann said, "Anyone 403who attempts to generate random numbers by deterministic means is, of 404course, living in a state of sin." 405 406Perl relies on the underlying system for the implementation of 407C<rand> and C<srand>; on some systems, the generated numbers are 408not random enough (especially on Windows : see 409L<http://www.perlmonks.org/?node_id=803632>). 410Several CPAN modules in the C<Math> namespace implement better 411pseudorandom generators; see for example 412L<Math::Random::MT> ("Mersenne Twister", fast), or 413L<Math::TrulyRandom> (uses the imperfections in the system's 414timer to generate random numbers, which is rather slow). 415More algorithms for random numbers are described in 416"Numerical Recipes in C" at L<http://www.nr.com/> 417 418=head2 How do I get a random number between X and Y? 419 420To get a random number between two values, you can use the C<rand()> 421built-in to get a random number between 0 and 1. From there, you shift 422that into the range that you want. 423 424C<rand($x)> returns a number such that C<< 0 <= rand($x) < $x >>. Thus 425what you want to have perl figure out is a random number in the range 426from 0 to the difference between your I<X> and I<Y>. 427 428That is, to get a number between 10 and 15, inclusive, you want a 429random number between 0 and 5 that you can then add to 10. 430 431 my $number = 10 + int rand( 15-10+1 ); # ( 10,11,12,13,14, or 15 ) 432 433Hence you derive the following simple function to abstract 434that. It selects a random integer between the two given 435integers (inclusive). For example: C<random_int_between(50,120)>. 436 437 sub random_int_between { 438 my($min, $max) = @_; 439 # Assumes that the two arguments are integers themselves! 440 return $min if $min == $max; 441 ($min, $max) = ($max, $min) if $min > $max; 442 return $min + int rand(1 + $max - $min); 443 } 444 445=head1 Data: Dates 446 447=head2 How do I find the day or week of the year? 448 449The day of the year is in the list returned 450by the C<localtime> function. Without an 451argument C<localtime> uses the current time. 452 453 my $day_of_year = (localtime)[7]; 454 455The L<POSIX> module can also format a date as the day of the year or 456week of the year. 457 458 use POSIX qw/strftime/; 459 my $day_of_year = strftime "%j", localtime; 460 my $week_of_year = strftime "%W", localtime; 461 462To get the day of year for any date, use L<POSIX>'s C<mktime> to get 463a time in epoch seconds for the argument to C<localtime>. 464 465 use POSIX qw/mktime strftime/; 466 my $week_of_year = strftime "%j", 467 localtime( mktime( 0, 0, 0, 18, 11, 87 ) ); 468 469You can also use L<Time::Piece>, which comes with Perl and provides a 470C<localtime> that returns an object: 471 472 use Time::Piece; 473 my $day_of_year = localtime->yday; 474 my $week_of_year = localtime->week; 475 476The L<Date::Calc> module provides two functions to calculate these, too: 477 478 use Date::Calc; 479 my $day_of_year = Day_of_Year( 1987, 12, 18 ); 480 my $week_of_year = Week_of_Year( 1987, 12, 18 ); 481 482=head2 How do I find the current century or millennium? 483 484Use the following simple functions: 485 486 sub get_century { 487 return int((((localtime(shift || time))[5] + 1999))/100); 488 } 489 490 sub get_millennium { 491 return 1+int((((localtime(shift || time))[5] + 1899))/1000); 492 } 493 494On some systems, the L<POSIX> module's C<strftime()> function has been 495extended in a non-standard way to use a C<%C> format, which they 496sometimes claim is the "century". It isn't, because on most such 497systems, this is only the first two digits of the four-digit year, and 498thus cannot be used to determine reliably the current century or 499millennium. 500 501=head2 How can I compare two dates and find the difference? 502 503(contributed by brian d foy) 504 505You could just store all your dates as a number and then subtract. 506Life isn't always that simple though. 507 508The L<Time::Piece> module, which comes with Perl, replaces L<localtime> 509with a version that returns an object. It also overloads the comparison 510operators so you can compare them directly: 511 512 use Time::Piece; 513 my $date1 = localtime( $some_time ); 514 my $date2 = localtime( $some_other_time ); 515 516 if( $date1 < $date2 ) { 517 print "The date was in the past\n"; 518 } 519 520You can also get differences with a subtraction, which returns a 521L<Time::Seconds> object: 522 523 my $date_diff = $date1 - $date2; 524 print "The difference is ", $date_diff->days, " days\n"; 525 526If you want to work with formatted dates, the L<Date::Manip>, 527L<Date::Calc>, or L<DateTime> modules can help you. 528 529=head2 How can I take a string and turn it into epoch seconds? 530 531If it's a regular enough string that it always has the same format, 532you can split it up and pass the parts to C<timelocal> in the standard 533L<Time::Local> module. Otherwise, you should look into the L<Date::Calc>, 534L<Date::Parse>, and L<Date::Manip> modules from CPAN. 535 536=head2 How can I find the Julian Day? 537 538(contributed by brian d foy and Dave Cross) 539 540You can use the L<Time::Piece> module, part of the Standard Library, 541which can convert a date/time to a Julian Day: 542 543 $ perl -MTime::Piece -le 'print localtime->julian_day' 544 2455607.7959375 545 546Or the modified Julian Day: 547 548 $ perl -MTime::Piece -le 'print localtime->mjd' 549 55607.2961226851 550 551Or even the day of the year (which is what some people think of as a 552Julian day): 553 554 $ perl -MTime::Piece -le 'print localtime->yday' 555 45 556 557You can also do the same things with the L<DateTime> module: 558 559 $ perl -MDateTime -le'print DateTime->today->jd' 560 2453401.5 561 $ perl -MDateTime -le'print DateTime->today->mjd' 562 53401 563 $ perl -MDateTime -le'print DateTime->today->doy' 564 31 565 566You can use the L<Time::JulianDay> module available on CPAN. Ensure 567that you really want to find a Julian day, though, as many people have 568different ideas about Julian days (see L<http://www.hermetic.ch/cal_stud/jdn.htm> 569for instance): 570 571 $ perl -MTime::JulianDay -le 'print local_julian_day( time )' 572 55608 573 574=head2 How do I find yesterday's date? 575X<date> X<yesterday> X<DateTime> X<Date::Calc> X<Time::Local> 576X<daylight saving time> X<day> X<Today_and_Now> X<localtime> 577X<timelocal> 578 579(contributed by brian d foy) 580 581To do it correctly, you can use one of the C<Date> modules since they 582work with calendars instead of times. The L<DateTime> module makes it 583simple, and give you the same time of day, only the day before, 584despite daylight saving time changes: 585 586 use DateTime; 587 588 my $yesterday = DateTime->now->subtract( days => 1 ); 589 590 print "Yesterday was $yesterday\n"; 591 592You can also use the L<Date::Calc> module using its C<Today_and_Now> 593function. 594 595 use Date::Calc qw( Today_and_Now Add_Delta_DHMS ); 596 597 my @date_time = Add_Delta_DHMS( Today_and_Now(), -1, 0, 0, 0 ); 598 599 print "@date_time\n"; 600 601Most people try to use the time rather than the calendar to figure out 602dates, but that assumes that days are twenty-four hours each. For 603most people, there are two days a year when they aren't: the switch to 604and from summer time throws this off. For example, the rest of the 605suggestions will be wrong sometimes: 606 607Starting with Perl 5.10, L<Time::Piece> and L<Time::Seconds> are part 608of the standard distribution, so you might think that you could do 609something like this: 610 611 use Time::Piece; 612 use Time::Seconds; 613 614 my $yesterday = localtime() - ONE_DAY; # WRONG 615 print "Yesterday was $yesterday\n"; 616 617The L<Time::Piece> module exports a new C<localtime> that returns an 618object, and L<Time::Seconds> exports the C<ONE_DAY> constant that is a 619set number of seconds. This means that it always gives the time 24 620hours ago, which is not always yesterday. This can cause problems 621around the end of daylight saving time when there's one day that is 25 622hours long. 623 624You have the same problem with L<Time::Local>, which will give the wrong 625answer for those same special cases: 626 627 # contributed by Gunnar Hjalmarsson 628 use Time::Local; 629 my $today = timelocal 0, 0, 12, ( localtime )[3..5]; 630 my ($d, $m, $y) = ( localtime $today-86400 )[3..5]; # WRONG 631 printf "Yesterday: %d-%02d-%02d\n", $y+1900, $m+1, $d; 632 633=head2 Does Perl have a Year 2000 or 2038 problem? Is Perl Y2K compliant? 634 635(contributed by brian d foy) 636 637Perl itself never had a Y2K problem, although that never stopped people 638from creating Y2K problems on their own. See the documentation for 639C<localtime> for its proper use. 640 641Starting with Perl 5.12, C<localtime> and C<gmtime> can handle dates past 64203:14:08 January 19, 2038, when a 32-bit based time would overflow. You 643still might get a warning on a 32-bit C<perl>: 644 645 % perl5.12 -E 'say scalar localtime( 0x9FFF_FFFFFFFF )' 646 Integer overflow in hexadecimal number at -e line 1. 647 Wed Nov 1 19:42:39 5576711 648 649On a 64-bit C<perl>, you can get even larger dates for those really long 650running projects: 651 652 % perl5.12 -E 'say scalar gmtime( 0x9FFF_FFFFFFFF )' 653 Thu Nov 2 00:42:39 5576711 654 655You're still out of luck if you need to keep track of decaying protons 656though. 657 658=head1 Data: Strings 659 660=head2 How do I validate input? 661 662(contributed by brian d foy) 663 664There are many ways to ensure that values are what you expect or 665want to accept. Besides the specific examples that we cover in the 666perlfaq, you can also look at the modules with "Assert" and "Validate" 667in their names, along with other modules such as L<Regexp::Common>. 668 669Some modules have validation for particular types of input, such 670as L<Business::ISBN>, L<Business::CreditCard>, L<Email::Valid>, 671and L<Data::Validate::IP>. 672 673=head2 How do I unescape a string? 674 675It depends just what you mean by "escape". URL escapes are dealt 676with in L<perlfaq9>. Shell escapes with the backslash (C<\>) 677character are removed with 678 679 s/\\(.)/$1/g; 680 681This won't expand C<"\n"> or C<"\t"> or any other special escapes. 682 683=head2 How do I remove consecutive pairs of characters? 684 685(contributed by brian d foy) 686 687You can use the substitution operator to find pairs of characters (or 688runs of characters) and replace them with a single instance. In this 689substitution, we find a character in C<(.)>. The memory parentheses 690store the matched character in the back-reference C<\g1> and we use 691that to require that the same thing immediately follow it. We replace 692that part of the string with the character in C<$1>. 693 694 s/(.)\g1/$1/g; 695 696We can also use the transliteration operator, C<tr///>. In this 697example, the search list side of our C<tr///> contains nothing, but 698the C<c> option complements that so it contains everything. The 699replacement list also contains nothing, so the transliteration is 700almost a no-op since it won't do any replacements (or more exactly, 701replace the character with itself). However, the C<s> option squashes 702duplicated and consecutive characters in the string so a character 703does not show up next to itself 704 705 my $str = 'Haarlem'; # in the Netherlands 706 $str =~ tr///cs; # Now Harlem, like in New York 707 708=head2 How do I expand function calls in a string? 709 710(contributed by brian d foy) 711 712This is documented in L<perlref>, and although it's not the easiest 713thing to read, it does work. In each of these examples, we call the 714function inside the braces used to dereference a reference. If we 715have more than one return value, we can construct and dereference an 716anonymous array. In this case, we call the function in list context. 717 718 print "The time values are @{ [localtime] }.\n"; 719 720If we want to call the function in scalar context, we have to do a bit 721more work. We can really have any code we like inside the braces, so 722we simply have to end with the scalar reference, although how you do 723that is up to you, and you can use code inside the braces. Note that 724the use of parens creates a list context, so we need C<scalar> to 725force the scalar context on the function: 726 727 print "The time is ${\(scalar localtime)}.\n" 728 729 print "The time is ${ my $x = localtime; \$x }.\n"; 730 731If your function already returns a reference, you don't need to create 732the reference yourself. 733 734 sub timestamp { my $t = localtime; \$t } 735 736 print "The time is ${ timestamp() }.\n"; 737 738The C<Interpolation> module can also do a lot of magic for you. You can 739specify a variable name, in this case C<E>, to set up a tied hash that 740does the interpolation for you. It has several other methods to do this 741as well. 742 743 use Interpolation E => 'eval'; 744 print "The time values are $E{localtime()}.\n"; 745 746In most cases, it is probably easier to simply use string concatenation, 747which also forces scalar context. 748 749 print "The time is " . localtime() . ".\n"; 750 751=head2 How do I find matching/nesting anything? 752 753To find something between two single 754characters, a pattern like C</x([^x]*)x/> will get the intervening 755bits in $1. For multiple ones, then something more like 756C</alpha(.*?)omega/> would be needed. For nested patterns 757and/or balanced expressions, see the so-called 758L<< (?PARNO)|perlre/C<(?PARNO)> C<(?-PARNO)> C<(?+PARNO)> C<(?R)> C<(?0)> >> 759construct (available since perl 5.10). 760The CPAN module L<Regexp::Common> can help to build such 761regular expressions (see in particular 762L<Regexp::Common::balanced> and L<Regexp::Common::delimited>). 763 764More complex cases will require to write a parser, probably 765using a parsing module from CPAN, like 766L<Regexp::Grammars>, L<Parse::RecDescent>, L<Parse::Yapp>, 767L<Text::Balanced>, or L<Marpa::R2>. 768 769=head2 How do I reverse a string? 770 771Use C<reverse()> in scalar context, as documented in 772L<perlfunc/reverse>. 773 774 my $reversed = reverse $string; 775 776=head2 How do I expand tabs in a string? 777 778You can do it yourself: 779 780 1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e; 781 782Or you can just use the L<Text::Tabs> module (part of the standard Perl 783distribution). 784 785 use Text::Tabs; 786 my @expanded_lines = expand(@lines_with_tabs); 787 788=head2 How do I reformat a paragraph? 789 790Use L<Text::Wrap> (part of the standard Perl distribution): 791 792 use Text::Wrap; 793 print wrap("\t", ' ', @paragraphs); 794 795The paragraphs you give to L<Text::Wrap> should not contain embedded 796newlines. L<Text::Wrap> doesn't justify the lines (flush-right). 797 798Or use the CPAN module L<Text::Autoformat>. Formatting files can be 799easily done by making a shell alias, like so: 800 801 alias fmt="perl -i -MText::Autoformat -n0777 \ 802 -e 'print autoformat $_, {all=>1}' $*" 803 804See the documentation for L<Text::Autoformat> to appreciate its many 805capabilities. 806 807=head2 How can I access or change N characters of a string? 808 809You can access the first characters of a string with substr(). 810To get the first character, for example, start at position 0 811and grab the string of length 1. 812 813 814 my $string = "Just another Perl Hacker"; 815 my $first_char = substr( $string, 0, 1 ); # 'J' 816 817To change part of a string, you can use the optional fourth 818argument which is the replacement string. 819 820 substr( $string, 13, 4, "Perl 5.8.0" ); 821 822You can also use substr() as an lvalue. 823 824 substr( $string, 13, 4 ) = "Perl 5.8.0"; 825 826=head2 How do I change the Nth occurrence of something? 827 828You have to keep track of N yourself. For example, let's say you want 829to change the fifth occurrence of C<"whoever"> or C<"whomever"> into 830C<"whosoever"> or C<"whomsoever">, case insensitively. These 831all assume that $_ contains the string to be altered. 832 833 $count = 0; 834 s{((whom?)ever)}{ 835 ++$count == 5 # is it the 5th? 836 ? "${2}soever" # yes, swap 837 : $1 # renege and leave it there 838 }ige; 839 840In the more general case, you can use the C</g> modifier in a C<while> 841loop, keeping count of matches. 842 843 $WANT = 3; 844 $count = 0; 845 $_ = "One fish two fish red fish blue fish"; 846 while (/(\w+)\s+fish\b/gi) { 847 if (++$count == $WANT) { 848 print "The third fish is a $1 one.\n"; 849 } 850 } 851 852That prints out: C<"The third fish is a red one."> You can also use a 853repetition count and repeated pattern like this: 854 855 /(?:\w+\s+fish\s+){2}(\w+)\s+fish/i; 856 857=head2 How can I count the number of occurrences of a substring within a string? 858 859There are a number of ways, with varying efficiency. If you want a 860count of a certain single character (X) within a string, you can use the 861C<tr///> function like so: 862 863 my $string = "ThisXlineXhasXsomeXx'sXinXit"; 864 my $count = ($string =~ tr/X//); 865 print "There are $count X characters in the string"; 866 867This is fine if you are just looking for a single character. However, 868if you are trying to count multiple character substrings within a 869larger string, C<tr///> won't work. What you can do is wrap a while() 870loop around a global pattern match. For example, let's count negative 871integers: 872 873 my $string = "-9 55 48 -2 23 -76 4 14 -44"; 874 my $count = 0; 875 while ($string =~ /-\d+/g) { $count++ } 876 print "There are $count negative numbers in the string"; 877 878Another version uses a global match in list context, then assigns the 879result to a scalar, producing a count of the number of matches. 880 881 my $count = () = $string =~ /-\d+/g; 882 883=head2 How do I capitalize all the words on one line? 884X<Text::Autoformat> X<capitalize> X<case, title> X<case, sentence> 885 886(contributed by brian d foy) 887 888Damian Conway's L<Text::Autoformat> handles all of the thinking 889for you: 890 891 use Text::Autoformat; 892 my $s = "Going to the desert with a camel"; 893 894 printf "%-10s %s\n", "original:", $s; 895 for my $style (qw( sentence title highlight )) { 896 printf "%-10s %s\n", 897 "$style:", 898 autoformat($s, { case => $style }) =~ s/\R+\z//r; 899 } 900 901Each style has a different idea of what if should do with small words: 902 903 original: Going to the desert with a camel 904 sentence: Going to the desert with a camel 905 title: Going To The Desert With A Camel 906 highlight: Going to the Desert with a Camel 907 908Trying this yourself in a simple regex has more than a few pitfalls. 909Perl "words" are groups of C<\w+>, but that's not what you want to 910capitalize. Some of those characters aren't even letters. What if you 911used a word boundary, C<\b>? 912 913 my $string = "A camel's journey"; 914 $string =~ s/\b(\w)/\U$1/g; # A Camel'S Journey 915 916How is Perl supposed to know not to capitalize that C<s>? You could work 917harder to look for preceding whitespace or the beginning of the string: 918 919 $string =~ s/( (?:^|\s) \w ) /\U$1/xg; # A Camel's Journey 920 921Or maybe you match the entire group, lowercase it all (C<L>), then 922uppercase the first letter (C<\u>): 923 924 $string =~ s/([\w']+)/\u\L$1/g; 925 926Each of these have their annoying edge cases. Perl v5.22 added fancier 927Unicode word and sentence boundaries to fix this sort of problem. The 928C<\b{wb}> word boundary ignores some things that it doesn't think start 929or end "human" words: 930 931 use v5.22; 932 my $string = "A camel's journey"; 933 $string =~ s/\b{wb}(\w)/\U$1/g; # A Camel's Journey 934 935The details are in Unicode Technical Report #29 936(L<https://unicode.org/reports/tr29/>), and you should make sure that 937its idea of a word boundary corresponds to yours. 938 939Now, what if you don't want to capitalize that "and" or "the"? Just use 940L<Text::Autoformat> and get on with the next problem. :) 941 942=head2 How can I split a [character]-delimited string except when inside [character]? 943 944Several modules can handle this sort of parsing--L<Text::Balanced>, 945L<Text::CSV>, L<Text::CSV_XS>, and L<Text::ParseWords>, among others. 946 947Take the example case of trying to split a string that is 948comma-separated into its different fields. You can't use C<split(/,/)> 949because you shouldn't split if the comma is inside quotes. For 950example, take a data line like this: 951 952 SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Error, Core Dumped" 953 954Due to the restriction of the quotes, this is a fairly complex 955problem. Thankfully, we have Jeffrey Friedl, author of 956I<Mastering Regular Expressions>, to handle these for us. He 957suggests (assuming your string is contained in C<$text>): 958 959 my @new = (); 960 push(@new, $+) while $text =~ m{ 961 "([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes 962 | ([^,]+),? 963 | , 964 }gx; 965 push(@new, undef) if substr($text,-1,1) eq ','; 966 967If you want to represent quotation marks inside a 968quotation-mark-delimited field, escape them with backslashes (eg, 969C<"like \"this\"">. 970 971Alternatively, the L<Text::ParseWords> module (part of the standard 972Perl distribution) lets you say: 973 974 use Text::ParseWords; 975 @new = quotewords(",", 0, $text); 976 977For parsing or generating CSV, though, using L<Text::CSV> rather than 978implementing it yourself is highly recommended; you'll save yourself odd bugs 979popping up later by just using code which has already been tried and tested in 980production for years. 981 982=head2 How do I strip blank space from the beginning/end of a string? 983 984(contributed by brian d foy) 985 986A substitution can do this for you. For a single line, you want to 987replace all the leading or trailing whitespace with nothing. You 988can do that with a pair of substitutions: 989 990 s/^\s+//; 991 s/\s+$//; 992 993You can also write that as a single substitution, although it turns 994out the combined statement is slower than the separate ones. That 995might not matter to you, though: 996 997 s/^\s+|\s+$//g; 998 999In this regular expression, the alternation matches either at the 1000beginning or the end of the string since the anchors have a lower 1001precedence than the alternation. With the C</g> flag, the substitution 1002makes all possible matches, so it gets both. Remember, the trailing 1003newline matches the C<\s+>, and the C<$> anchor can match to the 1004absolute end of the string, so the newline disappears too. Just add 1005the newline to the output, which has the added benefit of preserving 1006"blank" (consisting entirely of whitespace) lines which the C<^\s+> 1007would remove all by itself: 1008 1009 while( <> ) { 1010 s/^\s+|\s+$//g; 1011 print "$_\n"; 1012 } 1013 1014For a multi-line string, you can apply the regular expression to each 1015logical line in the string by adding the C</m> flag (for 1016"multi-line"). With the C</m> flag, the C<$> matches I<before> an 1017embedded newline, so it doesn't remove it. This pattern still removes 1018the newline at the end of the string: 1019 1020 $string =~ s/^\s+|\s+$//gm; 1021 1022Remember that lines consisting entirely of whitespace will disappear, 1023since the first part of the alternation can match the entire string 1024and replace it with nothing. If you need to keep embedded blank lines, 1025you have to do a little more work. Instead of matching any whitespace 1026(since that includes a newline), just match the other whitespace: 1027 1028 $string =~ s/^[\t\f ]+|[\t\f ]+$//mg; 1029 1030=head2 How do I pad a string with blanks or pad a number with zeroes? 1031 1032In the following examples, C<$pad_len> is the length to which you wish 1033to pad the string, C<$text> or C<$num> contains the string to be padded, 1034and C<$pad_char> contains the padding character. You can use a single 1035character string constant instead of the C<$pad_char> variable if you 1036know what it is in advance. And in the same way you can use an integer in 1037place of C<$pad_len> if you know the pad length in advance. 1038 1039The simplest method uses the C<sprintf> function. It can pad on the left 1040or right with blanks and on the left with zeroes and it will not 1041truncate the result. The C<pack> function can only pad strings on the 1042right with blanks and it will truncate the result to a maximum length of 1043C<$pad_len>. 1044 1045 # Left padding a string with blanks (no truncation): 1046 my $padded = sprintf("%${pad_len}s", $text); 1047 my $padded = sprintf("%*s", $pad_len, $text); # same thing 1048 1049 # Right padding a string with blanks (no truncation): 1050 my $padded = sprintf("%-${pad_len}s", $text); 1051 my $padded = sprintf("%-*s", $pad_len, $text); # same thing 1052 1053 # Left padding a number with 0 (no truncation): 1054 my $padded = sprintf("%0${pad_len}d", $num); 1055 my $padded = sprintf("%0*d", $pad_len, $num); # same thing 1056 1057 # Right padding a string with blanks using pack (will truncate): 1058 my $padded = pack("A$pad_len",$text); 1059 1060If you need to pad with a character other than blank or zero you can use 1061one of the following methods. They all generate a pad string with the 1062C<x> operator and combine that with C<$text>. These methods do 1063not truncate C<$text>. 1064 1065Left and right padding with any character, creating a new string: 1066 1067 my $padded = $pad_char x ( $pad_len - length( $text ) ) . $text; 1068 my $padded = $text . $pad_char x ( $pad_len - length( $text ) ); 1069 1070Left and right padding with any character, modifying C<$text> directly: 1071 1072 substr( $text, 0, 0 ) = $pad_char x ( $pad_len - length( $text ) ); 1073 $text .= $pad_char x ( $pad_len - length( $text ) ); 1074 1075=head2 How do I extract selected columns from a string? 1076 1077(contributed by brian d foy) 1078 1079If you know the columns that contain the data, you can 1080use C<substr> to extract a single column. 1081 1082 my $column = substr( $line, $start_column, $length ); 1083 1084You can use C<split> if the columns are separated by whitespace or 1085some other delimiter, as long as whitespace or the delimiter cannot 1086appear as part of the data. 1087 1088 my $line = ' fred barney betty '; 1089 my @columns = split /\s+/, $line; 1090 # ( '', 'fred', 'barney', 'betty' ); 1091 1092 my $line = 'fred||barney||betty'; 1093 my @columns = split /\|/, $line; 1094 # ( 'fred', '', 'barney', '', 'betty' ); 1095 1096If you want to work with comma-separated values, don't do this since 1097that format is a bit more complicated. Use one of the modules that 1098handle that format, such as L<Text::CSV>, L<Text::CSV_XS>, or 1099L<Text::CSV_PP>. 1100 1101If you want to break apart an entire line of fixed columns, you can use 1102C<unpack> with the A (ASCII) format. By using a number after the format 1103specifier, you can denote the column width. See the C<pack> and C<unpack> 1104entries in L<perlfunc> for more details. 1105 1106 my @fields = unpack( $line, "A8 A8 A8 A16 A4" ); 1107 1108Note that spaces in the format argument to C<unpack> do not denote literal 1109spaces. If you have space separated data, you may want C<split> instead. 1110 1111=head2 How do I find the soundex value of a string? 1112 1113(contributed by brian d foy) 1114 1115You can use the C<Text::Soundex> module. If you want to do fuzzy or close 1116matching, you might also try the L<String::Approx>, and 1117L<Text::Metaphone>, and L<Text::DoubleMetaphone> modules. 1118 1119=head2 How can I expand variables in text strings? 1120 1121(contributed by brian d foy) 1122 1123If you can avoid it, don't, or if you can use a templating system, 1124such as L<Text::Template> or L<Template> Toolkit, do that instead. You 1125might even be able to get the job done with C<sprintf> or C<printf>: 1126 1127 my $string = sprintf 'Say hello to %s and %s', $foo, $bar; 1128 1129However, for the one-off simple case where I don't want to pull out a 1130full templating system, I'll use a string that has two Perl scalar 1131variables in it. In this example, I want to expand C<$foo> and C<$bar> 1132to their variable's values: 1133 1134 my $foo = 'Fred'; 1135 my $bar = 'Barney'; 1136 $string = 'Say hello to $foo and $bar'; 1137 1138One way I can do this involves the substitution operator and a double 1139C</e> flag. The first C</e> evaluates C<$1> on the replacement side and 1140turns it into C<$foo>. The second /e starts with C<$foo> and replaces 1141it with its value. C<$foo>, then, turns into 'Fred', and that's finally 1142what's left in the string: 1143 1144 $string =~ s/(\$\w+)/$1/eeg; # 'Say hello to Fred and Barney' 1145 1146The C</e> will also silently ignore violations of strict, replacing 1147undefined variable names with the empty string. Since I'm using the 1148C</e> flag (twice even!), I have all of the same security problems I 1149have with C<eval> in its string form. If there's something odd in 1150C<$foo>, perhaps something like C<@{[ system "rm -rf /" ]}>, then 1151I could get myself in trouble. 1152 1153To get around the security problem, I could also pull the values from 1154a hash instead of evaluating variable names. Using a single C</e>, I 1155can check the hash to ensure the value exists, and if it doesn't, I 1156can replace the missing value with a marker, in this case C<???> to 1157signal that I missed something: 1158 1159 my $string = 'This has $foo and $bar'; 1160 1161 my %Replacements = ( 1162 foo => 'Fred', 1163 ); 1164 1165 # $string =~ s/\$(\w+)/$Replacements{$1}/g; 1166 $string =~ s/\$(\w+)/ 1167 exists $Replacements{$1} ? $Replacements{$1} : '???' 1168 /eg; 1169 1170 print $string; 1171 1172 1173=head2 Does Perl have anything like Ruby's #{} or Python's f string? 1174 1175Unlike the others, Perl allows you to embed a variable naked in a double 1176quoted string, e.g. C<"variable $variable">. When there isn't whitespace or 1177other non-word characters following the variable name, you can add braces 1178(e.g. C<"foo ${foo}bar">) to ensure correct parsing. 1179 1180An array can also be embedded directly in a string, and will be expanded 1181by default with spaces between the elements. The default 1182L<LIST_SEPARATOR|perlvar/$LIST_SEPARATOR> can be changed by assigning a 1183different string to the special variable C<$">, such as C<local $" = ', ';>. 1184 1185Perl also supports references within a string providing the equivalent of 1186the features in the other two languages. 1187 1188C<${\ ... }> embedded within a string will work for most simple statements 1189such as an object->method call. More complex code can be wrapped in a do 1190block C<${\ do{...} }>. 1191 1192When you want a list to be expanded per C<$">, use C<@{[ ... ]}>. 1193 1194 use Time::Piece; 1195 use Time::Seconds; 1196 my $scalar = 'STRING'; 1197 my @array = ( 'zorro', 'a', 1, 'B', 3 ); 1198 1199 # Print the current date and time and then Tommorrow 1200 my $t = Time::Piece->new; 1201 say "Now is: ${\ $t->cdate() }"; 1202 say "Tomorrow: ${\ do{ my $T=Time::Piece->new + ONE_DAY ; $T->fullday }}"; 1203 1204 # some variables in strings 1205 say "This is some scalar I have $scalar, this is an array @array."; 1206 say "You can also write it like this ${scalar} @{array}."; 1207 1208 # Change the $LIST_SEPARATOR 1209 local $" = ':'; 1210 say "Set \$\" to delimit with ':' and sort the Array @{[ sort @array ]}"; 1211 1212You may also want to look at the module 1213L<Quote::Code>, and templating tools such as L<Template::Toolkit> and 1214L<Mojo::Template>. 1215 1216See also: L</"How can I expand variables in text strings?"> and 1217L</"How do I expand function calls in a string?"> in this FAQ. 1218 1219=head2 What's wrong with always quoting "$vars"? 1220 1221The problem is that those double-quotes force 1222stringification--coercing numbers and references into strings--even 1223when you don't want them to be strings. Think of it this way: 1224double-quote expansion is used to produce new strings. If you already 1225have a string, why do you need more? 1226 1227If you get used to writing odd things like these: 1228 1229 print "$var"; # BAD 1230 my $new = "$old"; # BAD 1231 somefunc("$var"); # BAD 1232 1233You'll be in trouble. Those should (in 99.8% of the cases) be 1234the simpler and more direct: 1235 1236 print $var; 1237 my $new = $old; 1238 somefunc($var); 1239 1240Otherwise, besides slowing you down, you're going to break code when 1241the thing in the scalar is actually neither a string nor a number, but 1242a reference: 1243 1244 func(\@array); 1245 sub func { 1246 my $aref = shift; 1247 my $oref = "$aref"; # WRONG 1248 } 1249 1250You can also get into subtle problems on those few operations in Perl 1251that actually do care about the difference between a string and a 1252number, such as the magical C<++> autoincrement operator or the 1253syscall() function. 1254 1255Stringification also destroys arrays. 1256 1257 my @lines = `command`; 1258 print "@lines"; # WRONG - extra blanks 1259 print @lines; # right 1260 1261=head2 Why don't my E<lt>E<lt>HERE documents work? 1262 1263Here documents are found in L<perlop>. Check for these three things: 1264 1265=over 4 1266 1267=item There must be no space after the E<lt>E<lt> part. 1268 1269=item There (probably) should be a semicolon at the end of the opening token 1270 1271=item You can't (easily) have any space in front of the tag. 1272 1273=item There needs to be at least a line separator after the end token. 1274 1275=back 1276 1277If you want to indent the text in the here document, you 1278can do this: 1279 1280 # all in one 1281 (my $VAR = <<HERE_TARGET) =~ s/^\s+//gm; 1282 your text 1283 goes here 1284 HERE_TARGET 1285 1286But the HERE_TARGET must still be flush against the margin. 1287If you want that indented also, you'll have to quote 1288in the indentation. 1289 1290 (my $quote = <<' FINIS') =~ s/^\s+//gm; 1291 ...we will have peace, when you and all your works have 1292 perished--and the works of your dark master to whom you 1293 would deliver us. You are a liar, Saruman, and a corrupter 1294 of men's hearts. --Theoden in /usr/src/perl/taint.c 1295 FINIS 1296 $quote =~ s/\s+--/\n--/; 1297 1298A nice general-purpose fixer-upper function for indented here documents 1299follows. It expects to be called with a here document as its argument. 1300It looks to see whether each line begins with a common substring, and 1301if so, strips that substring off. Otherwise, it takes the amount of leading 1302whitespace found on the first line and removes that much off each 1303subsequent line. 1304 1305 sub fix { 1306 local $_ = shift; 1307 my ($white, $leader); # common whitespace and common leading string 1308 if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\g1\g2?.*\n)+$/) { 1309 ($white, $leader) = ($2, quotemeta($1)); 1310 } else { 1311 ($white, $leader) = (/^(\s+)/, ''); 1312 } 1313 s/^\s*?$leader(?:$white)?//gm; 1314 return $_; 1315 } 1316 1317This works with leading special strings, dynamically determined: 1318 1319 my $remember_the_main = fix<<' MAIN_INTERPRETER_LOOP'; 1320 @@@ int 1321 @@@ runops() { 1322 @@@ SAVEI32(runlevel); 1323 @@@ runlevel++; 1324 @@@ while ( op = (*op->op_ppaddr)() ); 1325 @@@ TAINT_NOT; 1326 @@@ return 0; 1327 @@@ } 1328 MAIN_INTERPRETER_LOOP 1329 1330Or with a fixed amount of leading whitespace, with remaining 1331indentation correctly preserved: 1332 1333 my $poem = fix<<EVER_ON_AND_ON; 1334 Now far ahead the Road has gone, 1335 And I must follow, if I can, 1336 Pursuing it with eager feet, 1337 Until it joins some larger way 1338 Where many paths and errands meet. 1339 And whither then? I cannot say. 1340 --Bilbo in /usr/src/perl/pp_ctl.c 1341 EVER_ON_AND_ON 1342 1343Beginning with Perl version 5.26, a much simpler and cleaner way to 1344write indented here documents has been added to the language: the 1345tilde (~) modifier. See L<perlop/"Indented Here-docs"> for details. 1346 1347=head1 Data: Arrays 1348 1349=head2 What is the difference between a list and an array? 1350 1351(contributed by brian d foy) 1352 1353A list is a fixed collection of scalars. An array is a variable that 1354holds a variable collection of scalars. An array can supply its collection 1355for list operations, so list operations also work on arrays: 1356 1357 # slices 1358 ( 'dog', 'cat', 'bird' )[2,3]; 1359 @animals[2,3]; 1360 1361 # iteration 1362 foreach ( qw( dog cat bird ) ) { ... } 1363 foreach ( @animals ) { ... } 1364 1365 my @three = grep { length == 3 } qw( dog cat bird ); 1366 my @three = grep { length == 3 } @animals; 1367 1368 # supply an argument list 1369 wash_animals( qw( dog cat bird ) ); 1370 wash_animals( @animals ); 1371 1372Array operations, which change the scalars, rearrange them, or add 1373or subtract some scalars, only work on arrays. These can't work on a 1374list, which is fixed. Array operations include C<shift>, C<unshift>, 1375C<push>, C<pop>, and C<splice>. 1376 1377An array can also change its length: 1378 1379 $#animals = 1; # truncate to two elements 1380 $#animals = 10000; # pre-extend to 10,001 elements 1381 1382You can change an array element, but you can't change a list element: 1383 1384 $animals[0] = 'Rottweiler'; 1385 qw( dog cat bird )[0] = 'Rottweiler'; # syntax error! 1386 1387 foreach ( @animals ) { 1388 s/^d/fr/; # works fine 1389 } 1390 1391 foreach ( qw( dog cat bird ) ) { 1392 s/^d/fr/; # Error! Modification of read only value! 1393 } 1394 1395However, if the list element is itself a variable, it appears that you 1396can change a list element. However, the list element is the variable, not 1397the data. You're not changing the list element, but something the list 1398element refers to. The list element itself doesn't change: it's still 1399the same variable. 1400 1401You also have to be careful about context. You can assign an array to 1402a scalar to get the number of elements in the array. This only works 1403for arrays, though: 1404 1405 my $count = @animals; # only works with arrays 1406 1407If you try to do the same thing with what you think is a list, you 1408get a quite different result. Although it looks like you have a list 1409on the righthand side, Perl actually sees a bunch of scalars separated 1410by a comma: 1411 1412 my $scalar = ( 'dog', 'cat', 'bird' ); # $scalar gets bird 1413 1414Since you're assigning to a scalar, the righthand side is in scalar 1415context. The comma operator (yes, it's an operator!) in scalar 1416context evaluates its lefthand side, throws away the result, and 1417evaluates its righthand side and returns the result. In effect, 1418that list-lookalike assigns to C<$scalar> its rightmost value. Many 1419people mess this up because they choose a list-lookalike whose 1420last element is also the count they expect: 1421 1422 my $scalar = ( 1, 2, 3 ); # $scalar gets 3, accidentally 1423 1424=head2 What is the difference between $array[1] and @array[1]? 1425 1426(contributed by brian d foy) 1427 1428The difference is the sigil, that special character in front of the 1429array name. The C<$> sigil means "exactly one item", while the C<@> 1430sigil means "zero or more items". The C<$> gets you a single scalar, 1431while the C<@> gets you a list. 1432 1433The confusion arises because people incorrectly assume that the sigil 1434denotes the variable type. 1435 1436The C<$array[1]> is a single-element access to the array. It's going 1437to return the item in index 1 (or undef if there is no item there). 1438If you intend to get exactly one element from the array, this is the 1439form you should use. 1440 1441The C<@array[1]> is an array slice, although it has only one index. 1442You can pull out multiple elements simultaneously by specifying 1443additional indices as a list, like C<@array[1,4,3,0]>. 1444 1445Using a slice on the lefthand side of the assignment supplies list 1446context to the righthand side. This can lead to unexpected results. 1447For instance, if you want to read a single line from a filehandle, 1448assigning to a scalar value is fine: 1449 1450 $array[1] = <STDIN>; 1451 1452However, in list context, the line input operator returns all of the 1453lines as a list. The first line goes into C<@array[1]> and the rest 1454of the lines mysteriously disappear: 1455 1456 @array[1] = <STDIN>; # most likely not what you want 1457 1458Either the C<use warnings> pragma or the B<-w> flag will warn you when 1459you use an array slice with a single index. 1460 1461=head2 How can I remove duplicate elements from a list or array? 1462 1463(contributed by brian d foy) 1464 1465Use a hash. When you think the words "unique" or "duplicated", think 1466"hash keys". 1467 1468If you don't care about the order of the elements, you could just 1469create the hash then extract the keys. It's not important how you 1470create that hash: just that you use C<keys> to get the unique 1471elements. 1472 1473 my %hash = map { $_, 1 } @array; 1474 # or a hash slice: @hash{ @array } = (); 1475 # or a foreach: $hash{$_} = 1 foreach ( @array ); 1476 1477 my @unique = keys %hash; 1478 1479If you want to use a module, try the C<uniq> function from 1480L<List::Util>. In list context it returns the unique elements, 1481preserving their order in the list. In scalar context, it returns the 1482number of unique elements. 1483 1484 use List::Util qw(uniq); 1485 1486 my @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 8 ); # 1,2,3,4,5,6,8 1487 my $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 8 ); # 7 1488 1489You can also go through each element and skip the ones you've seen 1490before. Use a hash to keep track. The first time the loop sees an 1491element, that element has no key in C<%seen>. The C<next> statement 1492creates the key and immediately uses its value, which is C<undef>, so 1493the loop continues to the C<push> and increments the value for that 1494key. The next time the loop sees that same element, its key exists in 1495the hash I<and> the value for that key is true (since it's not 0 or 1496C<undef>), so the next skips that iteration and the loop goes to the 1497next element. 1498 1499 my @unique = (); 1500 my %seen = (); 1501 1502 foreach my $elem ( @array ) { 1503 next if $seen{ $elem }++; 1504 push @unique, $elem; 1505 } 1506 1507You can write this more briefly using a grep, which does the 1508same thing. 1509 1510 my %seen = (); 1511 my @unique = grep { ! $seen{ $_ }++ } @array; 1512 1513=head2 How can I tell whether a certain element is contained in a list or array? 1514 1515(portions of this answer contributed by Anno Siegel and brian d foy) 1516 1517Hearing the word "in" is an I<in>dication that you probably should have 1518used a hash, not a list or array, to store your data. Hashes are 1519designed to answer this question quickly and efficiently. Arrays aren't. 1520 1521That being said, there are several ways to approach this. If you 1522are going to make this query many times over arbitrary string values, 1523the fastest way is probably to invert the original array and maintain a 1524hash whose keys are the first array's values: 1525 1526 my @blues = qw/azure cerulean teal turquoise lapis-lazuli/; 1527 my %is_blue = (); 1528 for (@blues) { $is_blue{$_} = 1 } 1529 1530Now you can check whether C<$is_blue{$some_color}>. It might have 1531been a good idea to keep the blues all in a hash in the first place. 1532 1533If the values are all small integers, you could use a simple indexed 1534array. This kind of an array will take up less space: 1535 1536 my @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31); 1537 my @is_tiny_prime = (); 1538 for (@primes) { $is_tiny_prime[$_] = 1 } 1539 # or simply @istiny_prime[@primes] = (1) x @primes; 1540 1541Now you check whether $is_tiny_prime[$some_number]. 1542 1543If the values in question are integers instead of strings, you can save 1544quite a lot of space by using bit strings instead: 1545 1546 my @articles = ( 1..10, 150..2000, 2017 ); 1547 undef $read; 1548 for (@articles) { vec($read,$_,1) = 1 } 1549 1550Now check whether C<vec($read,$n,1)> is true for some C<$n>. 1551 1552These methods guarantee fast individual tests but require a re-organization 1553of the original list or array. They only pay off if you have to test 1554multiple values against the same array. 1555 1556If you are testing only once, the standard module L<List::Util> exports 1557the function C<any> for this purpose. It works by stopping once it 1558finds the element. It's written in C for speed, and its Perl equivalent 1559looks like this subroutine: 1560 1561 sub any (&@) { 1562 my $code = shift; 1563 foreach (@_) { 1564 return 1 if $code->(); 1565 } 1566 return 0; 1567 } 1568 1569If speed is of little concern, the common idiom uses grep in scalar context 1570(which returns the number of items that passed its condition) to traverse the 1571entire list. This does have the benefit of telling you how many matches it 1572found, though. 1573 1574 my $is_there = grep $_ eq $whatever, @array; 1575 1576If you want to actually extract the matching elements, simply use grep in 1577list context. 1578 1579 my @matches = grep $_ eq $whatever, @array; 1580 1581=head2 How do I compute the difference of two arrays? How do I compute the intersection of two arrays? 1582 1583Use a hash. Here's code to do both and more. It assumes that each 1584element is unique in a given array: 1585 1586 my (@union, @intersection, @difference); 1587 my %count = (); 1588 foreach my $element (@array1, @array2) { $count{$element}++ } 1589 foreach my $element (keys %count) { 1590 push @union, $element; 1591 push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element; 1592 } 1593 1594Note that this is the I<symmetric difference>, that is, all elements 1595in either A or in B but not in both. Think of it as an xor operation. 1596 1597=head2 How do I test whether two arrays or hashes are equal? 1598 1599The following code works for single-level arrays. It uses a 1600stringwise comparison, and does not distinguish defined versus 1601undefined empty strings. Modify if you have other needs. 1602 1603 $are_equal = compare_arrays(\@frogs, \@toads); 1604 1605 sub compare_arrays { 1606 my ($first, $second) = @_; 1607 no warnings; # silence spurious -w undef complaints 1608 return 0 unless @$first == @$second; 1609 for (my $i = 0; $i < @$first; $i++) { 1610 return 0 if $first->[$i] ne $second->[$i]; 1611 } 1612 return 1; 1613 } 1614 1615For multilevel structures, you may wish to use an approach more 1616like this one. It uses the CPAN module L<FreezeThaw>: 1617 1618 use FreezeThaw qw(cmpStr); 1619 my @a = my @b = ( "this", "that", [ "more", "stuff" ] ); 1620 1621 printf "a and b contain %s arrays\n", 1622 cmpStr(\@a, \@b) == 0 1623 ? "the same" 1624 : "different"; 1625 1626This approach also works for comparing hashes. Here we'll demonstrate 1627two different answers: 1628 1629 use FreezeThaw qw(cmpStr cmpStrHard); 1630 1631 my %a = my %b = ( "this" => "that", "extra" => [ "more", "stuff" ] ); 1632 $a{EXTRA} = \%b; 1633 $b{EXTRA} = \%a; 1634 1635 printf "a and b contain %s hashes\n", 1636 cmpStr(\%a, \%b) == 0 ? "the same" : "different"; 1637 1638 printf "a and b contain %s hashes\n", 1639 cmpStrHard(\%a, \%b) == 0 ? "the same" : "different"; 1640 1641 1642The first reports that both those the hashes contain the same data, 1643while the second reports that they do not. Which you prefer is left as 1644an exercise to the reader. 1645 1646=head2 How do I find the first array element for which a condition is true? 1647 1648To find the first array element which satisfies a condition, you can 1649use the C<first()> function in the L<List::Util> module, which comes 1650with Perl 5.8. This example finds the first element that contains 1651"Perl". 1652 1653 use List::Util qw(first); 1654 1655 my $element = first { /Perl/ } @array; 1656 1657If you cannot use L<List::Util>, you can make your own loop to do the 1658same thing. Once you find the element, you stop the loop with last. 1659 1660 my $found; 1661 foreach ( @array ) { 1662 if( /Perl/ ) { $found = $_; last } 1663 } 1664 1665If you want the array index, use the C<firstidx()> function from 1666C<List::MoreUtils>: 1667 1668 use List::MoreUtils qw(firstidx); 1669 my $index = firstidx { /Perl/ } @array; 1670 1671Or write it yourself, iterating through the indices 1672and checking the array element at each index until you find one 1673that satisfies the condition: 1674 1675 my( $found, $index ) = ( undef, -1 ); 1676 for( $i = 0; $i < @array; $i++ ) { 1677 if( $array[$i] =~ /Perl/ ) { 1678 $found = $array[$i]; 1679 $index = $i; 1680 last; 1681 } 1682 } 1683 1684=head2 How do I handle linked lists? 1685 1686(contributed by brian d foy) 1687 1688Perl's arrays do not have a fixed size, so you don't need linked lists 1689if you just want to add or remove items. You can use array operations 1690such as C<push>, C<pop>, C<shift>, C<unshift>, or C<splice> to do 1691that. 1692 1693Sometimes, however, linked lists can be useful in situations where you 1694want to "shard" an array so you have many small arrays instead of 1695a single big array. You can keep arrays longer than Perl's largest 1696array index, lock smaller arrays separately in threaded programs, 1697reallocate less memory, or quickly insert elements in the middle of 1698the chain. 1699 1700Steve Lembark goes through the details in his YAPC::NA 2009 talk "Perly 1701Linked Lists" ( L<http://www.slideshare.net/lembark/perly-linked-lists> ), 1702although you can just use his L<LinkedList::Single> module. 1703 1704=head2 How do I handle circular lists? 1705X<circular> X<array> X<Tie::Cycle> X<Array::Iterator::Circular> 1706X<cycle> X<modulus> 1707 1708(contributed by brian d foy) 1709 1710If you want to cycle through an array endlessly, you can increment the 1711index modulo the number of elements in the array: 1712 1713 my @array = qw( a b c ); 1714 my $i = 0; 1715 1716 while( 1 ) { 1717 print $array[ $i++ % @array ], "\n"; 1718 last if $i > 20; 1719 } 1720 1721You can also use L<Tie::Cycle> to use a scalar that always has the 1722next element of the circular array: 1723 1724 use Tie::Cycle; 1725 1726 tie my $cycle, 'Tie::Cycle', [ qw( FFFFFF 000000 FFFF00 ) ]; 1727 1728 print $cycle; # FFFFFF 1729 print $cycle; # 000000 1730 print $cycle; # FFFF00 1731 1732The L<Array::Iterator::Circular> creates an iterator object for 1733circular arrays: 1734 1735 use Array::Iterator::Circular; 1736 1737 my $color_iterator = Array::Iterator::Circular->new( 1738 qw(red green blue orange) 1739 ); 1740 1741 foreach ( 1 .. 20 ) { 1742 print $color_iterator->next, "\n"; 1743 } 1744 1745=head2 How do I shuffle an array randomly? 1746 1747If you either have Perl 5.8.0 or later installed, or if you have 1748Scalar-List-Utils 1.03 or later installed, you can say: 1749 1750 use List::Util 'shuffle'; 1751 1752 @shuffled = shuffle(@list); 1753 1754If not, you can use a Fisher-Yates shuffle. 1755 1756 sub fisher_yates_shuffle { 1757 my $deck = shift; # $deck is a reference to an array 1758 return unless @$deck; # must not be empty! 1759 1760 my $i = @$deck; 1761 while (--$i) { 1762 my $j = int rand ($i+1); 1763 @$deck[$i,$j] = @$deck[$j,$i]; 1764 } 1765 } 1766 1767 # shuffle my mpeg collection 1768 # 1769 my @mpeg = <audio/*/*.mp3>; 1770 fisher_yates_shuffle( \@mpeg ); # randomize @mpeg in place 1771 print @mpeg; 1772 1773Note that the above implementation shuffles an array in place, 1774unlike the C<List::Util::shuffle()> which takes a list and returns 1775a new shuffled list. 1776 1777You've probably seen shuffling algorithms that work using splice, 1778randomly picking another element to swap the current element with 1779 1780 srand; 1781 @new = (); 1782 @old = 1 .. 10; # just a demo 1783 while (@old) { 1784 push(@new, splice(@old, rand @old, 1)); 1785 } 1786 1787This is bad because splice is already O(N), and since you do it N 1788times, you just invented a quadratic algorithm; that is, O(N**2). 1789This does not scale, although Perl is so efficient that you probably 1790won't notice this until you have rather largish arrays. 1791 1792=head2 How do I process/modify each element of an array? 1793 1794Use C<for>/C<foreach>: 1795 1796 for (@lines) { 1797 s/foo/bar/; # change that word 1798 tr/XZ/ZX/; # swap those letters 1799 } 1800 1801Here's another; let's compute spherical volumes: 1802 1803 my @volumes = @radii; 1804 for (@volumes) { # @volumes has changed parts 1805 $_ **= 3; 1806 $_ *= (4/3) * 3.14159; # this will be constant folded 1807 } 1808 1809which can also be done with C<map()> which is made to transform 1810one list into another: 1811 1812 my @volumes = map {$_ ** 3 * (4/3) * 3.14159} @radii; 1813 1814If you want to do the same thing to modify the values of the 1815hash, you can use the C<values> function. As of Perl 5.6 1816the values are not copied, so if you modify $orbit (in this 1817case), you modify the value. 1818 1819 for my $orbit ( values %orbits ) { 1820 ($orbit **= 3) *= (4/3) * 3.14159; 1821 } 1822 1823Prior to perl 5.6 C<values> returned copies of the values, 1824so older perl code often contains constructions such as 1825C<@orbits{keys %orbits}> instead of C<values %orbits> where 1826the hash is to be modified. 1827 1828=head2 How do I select a random element from an array? 1829 1830Use the C<rand()> function (see L<perlfunc/rand>): 1831 1832 my $index = rand @array; 1833 my $element = $array[$index]; 1834 1835Or, simply: 1836 1837 my $element = $array[ rand @array ]; 1838 1839=head2 How do I permute N elements of a list? 1840X<List::Permutor> X<permute> X<Algorithm::Loops> X<Knuth> 1841X<The Art of Computer Programming> X<Fischer-Krause> 1842 1843Use the L<List::Permutor> module on CPAN. If the list is actually an 1844array, try the L<Algorithm::Permute> module (also on CPAN). It's 1845written in XS code and is very efficient: 1846 1847 use Algorithm::Permute; 1848 1849 my @array = 'a'..'d'; 1850 my $p_iterator = Algorithm::Permute->new ( \@array ); 1851 1852 while (my @perm = $p_iterator->next) { 1853 print "next permutation: (@perm)\n"; 1854 } 1855 1856For even faster execution, you could do: 1857 1858 use Algorithm::Permute; 1859 1860 my @array = 'a'..'d'; 1861 1862 Algorithm::Permute::permute { 1863 print "next permutation: (@array)\n"; 1864 } @array; 1865 1866Here's a little program that generates all permutations of all the 1867words on each line of input. The algorithm embodied in the 1868C<permute()> function is discussed in Volume 4 (still unpublished) of 1869Knuth's I<The Art of Computer Programming> and will work on any list: 1870 1871 #!/usr/bin/perl -n 1872 # Fischer-Krause ordered permutation generator 1873 1874 sub permute (&@) { 1875 my $code = shift; 1876 my @idx = 0..$#_; 1877 while ( $code->(@_[@idx]) ) { 1878 my $p = $#idx; 1879 --$p while $idx[$p-1] > $idx[$p]; 1880 my $q = $p or return; 1881 push @idx, reverse splice @idx, $p; 1882 ++$q while $idx[$p-1] > $idx[$q]; 1883 @idx[$p-1,$q]=@idx[$q,$p-1]; 1884 } 1885 } 1886 1887 permute { print "@_\n" } split; 1888 1889The L<Algorithm::Loops> module also provides the C<NextPermute> and 1890C<NextPermuteNum> functions which efficiently find all unique permutations 1891of an array, even if it contains duplicate values, modifying it in-place: 1892if its elements are in reverse-sorted order then the array is reversed, 1893making it sorted, and it returns false; otherwise the next 1894permutation is returned. 1895 1896C<NextPermute> uses string order and C<NextPermuteNum> numeric order, so 1897you can enumerate all the permutations of C<0..9> like this: 1898 1899 use Algorithm::Loops qw(NextPermuteNum); 1900 1901 my @list= 0..9; 1902 do { print "@list\n" } while NextPermuteNum @list; 1903 1904=head2 How do I sort an array by (anything)? 1905 1906Supply a comparison function to sort() (described in L<perlfunc/sort>): 1907 1908 @list = sort { $a <=> $b } @list; 1909 1910The default sort function is cmp, string comparison, which would 1911sort C<(1, 2, 10)> into C<(1, 10, 2)>. C<< <=> >>, used above, is 1912the numerical comparison operator. 1913 1914If you have a complicated function needed to pull out the part you 1915want to sort on, then don't do it inside the sort function. Pull it 1916out first, because the sort BLOCK can be called many times for the 1917same element. Here's an example of how to pull out the first word 1918after the first number on each item, and then sort those words 1919case-insensitively. 1920 1921 my @idx; 1922 for (@data) { 1923 my $item; 1924 ($item) = /\d+\s*(\S+)/; 1925 push @idx, uc($item); 1926 } 1927 my @sorted = @data[ sort { $idx[$a] cmp $idx[$b] } 0 .. $#idx ]; 1928 1929which could also be written this way, using a trick 1930that's come to be known as the Schwartzian Transform: 1931 1932 my @sorted = map { $_->[0] } 1933 sort { $a->[1] cmp $b->[1] } 1934 map { [ $_, uc( (/\d+\s*(\S+)/)[0]) ] } @data; 1935 1936If you need to sort on several fields, the following paradigm is useful. 1937 1938 my @sorted = sort { 1939 field1($a) <=> field1($b) || 1940 field2($a) cmp field2($b) || 1941 field3($a) cmp field3($b) 1942 } @data; 1943 1944This can be conveniently combined with precalculation of keys as given 1945above. 1946 1947See the F<sort> article in the "Far More Than You Ever Wanted 1948To Know" collection in L<http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz> for 1949more about this approach. 1950 1951See also the question later in L<perlfaq4> on sorting hashes. 1952 1953=head2 How do I manipulate arrays of bits? 1954 1955Use C<pack()> and C<unpack()>, or else C<vec()> and the bitwise 1956operations. 1957 1958For example, you don't have to store individual bits in an array 1959(which would mean that you're wasting a lot of space). To convert an 1960array of bits to a string, use C<vec()> to set the right bits. This 1961sets C<$vec> to have bit N set only if C<$ints[N]> was set: 1962 1963 my @ints = (...); # array of bits, e.g. ( 1, 0, 0, 1, 1, 0 ... ) 1964 my $vec = ''; 1965 foreach( 0 .. $#ints ) { 1966 vec($vec,$_,1) = 1 if $ints[$_]; 1967 } 1968 1969The string C<$vec> only takes up as many bits as it needs. For 1970instance, if you had 16 entries in C<@ints>, C<$vec> only needs two 1971bytes to store them (not counting the scalar variable overhead). 1972 1973Here's how, given a vector in C<$vec>, you can get those bits into 1974your C<@ints> array: 1975 1976 sub bitvec_to_list { 1977 my $vec = shift; 1978 my @ints; 1979 # Find null-byte density then select best algorithm 1980 if ($vec =~ tr/\0// / length $vec > 0.95) { 1981 use integer; 1982 my $i; 1983 1984 # This method is faster with mostly null-bytes 1985 while($vec =~ /[^\0]/g ) { 1986 $i = -9 + 8 * pos $vec; 1987 push @ints, $i if vec($vec, ++$i, 1); 1988 push @ints, $i if vec($vec, ++$i, 1); 1989 push @ints, $i if vec($vec, ++$i, 1); 1990 push @ints, $i if vec($vec, ++$i, 1); 1991 push @ints, $i if vec($vec, ++$i, 1); 1992 push @ints, $i if vec($vec, ++$i, 1); 1993 push @ints, $i if vec($vec, ++$i, 1); 1994 push @ints, $i if vec($vec, ++$i, 1); 1995 } 1996 } 1997 else { 1998 # This method is a fast general algorithm 1999 use integer; 2000 my $bits = unpack "b*", $vec; 2001 push @ints, 0 if $bits =~ s/^(\d)// && $1; 2002 push @ints, pos $bits while($bits =~ /1/g); 2003 } 2004 2005 return \@ints; 2006 } 2007 2008This method gets faster the more sparse the bit vector is. 2009(Courtesy of Tim Bunce and Winfried Koenig.) 2010 2011You can make the while loop a lot shorter with this suggestion 2012from Benjamin Goldberg: 2013 2014 while($vec =~ /[^\0]+/g ) { 2015 push @ints, grep vec($vec, $_, 1), $-[0] * 8 .. $+[0] * 8; 2016 } 2017 2018Or use the CPAN module L<Bit::Vector>: 2019 2020 my $vector = Bit::Vector->new($num_of_bits); 2021 $vector->Index_List_Store(@ints); 2022 my @ints = $vector->Index_List_Read(); 2023 2024L<Bit::Vector> provides efficient methods for bit vector, sets of 2025small integers and "big int" math. 2026 2027Here's a more extensive illustration using vec(): 2028 2029 # vec demo 2030 my $vector = "\xff\x0f\xef\xfe"; 2031 print "Ilya's string \\xff\\x0f\\xef\\xfe represents the number ", 2032 unpack("N", $vector), "\n"; 2033 my $is_set = vec($vector, 23, 1); 2034 print "Its 23rd bit is ", $is_set ? "set" : "clear", ".\n"; 2035 pvec($vector); 2036 2037 set_vec(1,1,1); 2038 set_vec(3,1,1); 2039 set_vec(23,1,1); 2040 2041 set_vec(3,1,3); 2042 set_vec(3,2,3); 2043 set_vec(3,4,3); 2044 set_vec(3,4,7); 2045 set_vec(3,8,3); 2046 set_vec(3,8,7); 2047 2048 set_vec(0,32,17); 2049 set_vec(1,32,17); 2050 2051 sub set_vec { 2052 my ($offset, $width, $value) = @_; 2053 my $vector = ''; 2054 vec($vector, $offset, $width) = $value; 2055 print "offset=$offset width=$width value=$value\n"; 2056 pvec($vector); 2057 } 2058 2059 sub pvec { 2060 my $vector = shift; 2061 my $bits = unpack("b*", $vector); 2062 my $i = 0; 2063 my $BASE = 8; 2064 2065 print "vector length in bytes: ", length($vector), "\n"; 2066 @bytes = unpack("A8" x length($vector), $bits); 2067 print "bits are: @bytes\n\n"; 2068 } 2069 2070=head2 Why does defined() return true on empty arrays and hashes? 2071 2072The short story is that you should probably only use defined on scalars or 2073functions, not on aggregates (arrays and hashes). See L<perlfunc/defined> 2074in the 5.004 release or later of Perl for more detail. 2075 2076=head1 Data: Hashes (Associative Arrays) 2077 2078=head2 How do I process an entire hash? 2079 2080(contributed by brian d foy) 2081 2082There are a couple of ways that you can process an entire hash. You 2083can get a list of keys, then go through each key, or grab a one 2084key-value pair at a time. 2085 2086To go through all of the keys, use the C<keys> function. This extracts 2087all of the keys of the hash and gives them back to you as a list. You 2088can then get the value through the particular key you're processing: 2089 2090 foreach my $key ( keys %hash ) { 2091 my $value = $hash{$key} 2092 ... 2093 } 2094 2095Once you have the list of keys, you can process that list before you 2096process the hash elements. For instance, you can sort the keys so you 2097can process them in lexical order: 2098 2099 foreach my $key ( sort keys %hash ) { 2100 my $value = $hash{$key} 2101 ... 2102 } 2103 2104Or, you might want to only process some of the items. If you only want 2105to deal with the keys that start with C<text:>, you can select just 2106those using C<grep>: 2107 2108 foreach my $key ( grep /^text:/, keys %hash ) { 2109 my $value = $hash{$key} 2110 ... 2111 } 2112 2113If the hash is very large, you might not want to create a long list of 2114keys. To save some memory, you can grab one key-value pair at a time using 2115C<each()>, which returns a pair you haven't seen yet: 2116 2117 while( my( $key, $value ) = each( %hash ) ) { 2118 ... 2119 } 2120 2121The C<each> operator returns the pairs in apparently random order, so if 2122ordering matters to you, you'll have to stick with the C<keys> method. 2123 2124The C<each()> operator can be a bit tricky though. You can't add or 2125delete keys of the hash while you're using it without possibly 2126skipping or re-processing some pairs after Perl internally rehashes 2127all of the elements. Additionally, a hash has only one iterator, so if 2128you mix C<keys>, C<values>, or C<each> on the same hash, you risk resetting 2129the iterator and messing up your processing. See the C<each> entry in 2130L<perlfunc> for more details. 2131 2132=head2 How do I merge two hashes? 2133X<hash> X<merge> X<slice, hash> 2134 2135(contributed by brian d foy) 2136 2137Before you decide to merge two hashes, you have to decide what to do 2138if both hashes contain keys that are the same and if you want to leave 2139the original hashes as they were. 2140 2141If you want to preserve the original hashes, copy one hash (C<%hash1>) 2142to a new hash (C<%new_hash>), then add the keys from the other hash 2143(C<%hash2> to the new hash. Checking that the key already exists in 2144C<%new_hash> gives you a chance to decide what to do with the 2145duplicates: 2146 2147 my %new_hash = %hash1; # make a copy; leave %hash1 alone 2148 2149 foreach my $key2 ( keys %hash2 ) { 2150 if( exists $new_hash{$key2} ) { 2151 warn "Key [$key2] is in both hashes!"; 2152 # handle the duplicate (perhaps only warning) 2153 ... 2154 next; 2155 } 2156 else { 2157 $new_hash{$key2} = $hash2{$key2}; 2158 } 2159 } 2160 2161If you don't want to create a new hash, you can still use this looping 2162technique; just change the C<%new_hash> to C<%hash1>. 2163 2164 foreach my $key2 ( keys %hash2 ) { 2165 if( exists $hash1{$key2} ) { 2166 warn "Key [$key2] is in both hashes!"; 2167 # handle the duplicate (perhaps only warning) 2168 ... 2169 next; 2170 } 2171 else { 2172 $hash1{$key2} = $hash2{$key2}; 2173 } 2174 } 2175 2176If you don't care that one hash overwrites keys and values from the other, you 2177could just use a hash slice to add one hash to another. In this case, values 2178from C<%hash2> replace values from C<%hash1> when they have keys in common: 2179 2180 @hash1{ keys %hash2 } = values %hash2; 2181 2182=head2 What happens if I add or remove keys from a hash while iterating over it? 2183 2184(contributed by brian d foy) 2185 2186The easy answer is "Don't do that!" 2187 2188If you iterate through the hash with each(), you can delete the key 2189most recently returned without worrying about it. If you delete or add 2190other keys, the iterator may skip or double up on them since perl 2191may rearrange the hash table. See the 2192entry for C<each()> in L<perlfunc>. 2193 2194=head2 How do I look up a hash element by value? 2195 2196Create a reverse hash: 2197 2198 my %by_value = reverse %by_key; 2199 my $key = $by_value{$value}; 2200 2201That's not particularly efficient. It would be more space-efficient 2202to use: 2203 2204 while (my ($key, $value) = each %by_key) { 2205 $by_value{$value} = $key; 2206 } 2207 2208If your hash could have repeated values, the methods above will only find 2209one of the associated keys. This may or may not worry you. If it does 2210worry you, you can always reverse the hash into a hash of arrays instead: 2211 2212 while (my ($key, $value) = each %by_key) { 2213 push @{$key_list_by_value{$value}}, $key; 2214 } 2215 2216=head2 How can I know how many entries are in a hash? 2217 2218(contributed by brian d foy) 2219 2220This is very similar to "How do I process an entire hash?", also in 2221L<perlfaq4>, but a bit simpler in the common cases. 2222 2223You can use the C<keys()> built-in function in scalar context to find out 2224how many entries you have in a hash: 2225 2226 my $key_count = keys %hash; # must be scalar context! 2227 2228If you want to find out how many entries have a defined value, that's 2229a bit different. You have to check each value. A C<grep> is handy: 2230 2231 my $defined_value_count = grep { defined } values %hash; 2232 2233You can use that same structure to count the entries any way that 2234you like. If you want the count of the keys with vowels in them, 2235you just test for that instead: 2236 2237 my $vowel_count = grep { /[aeiou]/ } keys %hash; 2238 2239The C<grep> in scalar context returns the count. If you want the list 2240of matching items, just use it in list context instead: 2241 2242 my @defined_values = grep { defined } values %hash; 2243 2244The C<keys()> function also resets the iterator, which means that you may 2245see strange results if you use this between uses of other hash operators 2246such as C<each()>. 2247 2248=head2 How do I sort a hash (optionally by value instead of key)? 2249 2250(contributed by brian d foy) 2251 2252To sort a hash, start with the keys. In this example, we give the list of 2253keys to the sort function which then compares them as strings. The output list 2254has the keys in string order. Once we have the keys, we can go through them to 2255create a report which lists the keys in string order: 2256 2257 my @keys = sort { $a cmp $b } keys %hash; 2258 2259 foreach my $key ( @keys ) { 2260 printf "%-20s %6d\n", $key, $hash{$key}; 2261 } 2262 2263We could get more fancy in the C<sort()> block though. Instead of 2264comparing the keys, we can compute a value with them and use that 2265value as the comparison. 2266 2267For instance, to make our report order case-insensitive, we use 2268C<fc> to safely lowercase the keys before comparing them: 2269 2270 use v5.16; 2271 my @keys = sort { fc($a) cmp fc($b) } keys %hash; 2272 2273Earlier versions of this answer used C<lc>, but that could give 2274unexpected results with some Unicode strings. See L<perlfunc/fc> 2275for the details. The Unicode::UCD module does the same thing for 2276earlier perls. 2277 2278Note: if the computation is expensive or the hash has many elements, 2279you may want to look at the Schwartzian Transform to cache the 2280computation results. 2281 2282If we want to sort by the hash value instead, we use the hash key 2283to look it up. We still get out a list of keys, but this time they 2284are ordered by their value: 2285 2286 my @keys = sort { $hash{$a} <=> $hash{$b} } keys %hash; 2287 2288From there we can get more complex. If the hash values are the same, 2289we can provide a secondary sort on the hash key: 2290 2291 use v5.16; 2292 my @keys = sort { 2293 $hash{$a} <=> $hash{$b} 2294 or 2295 fc($a) cmp fc($b) 2296 } keys %hash; 2297 2298=head2 How can I always keep my hash sorted? 2299X<hash tie sort DB_File Tie::IxHash> 2300 2301You can look into using the C<DB_File> module and C<tie()> using the 2302C<$DB_BTREE> hash bindings as documented in L<DB_File/"In Memory 2303Databases">. The L<Tie::IxHash> module from CPAN might also be 2304instructive. Although this does keep your hash sorted, you might not 2305like the slowdown you suffer from the tie interface. Are you sure you 2306need to do this? :) 2307 2308=head2 What's the difference between "delete" and "undef" with hashes? 2309 2310Hashes contain pairs of scalars: the first is the key, the 2311second is the value. The key will be coerced to a string, 2312although the value can be any kind of scalar: string, 2313number, or reference. If a key C<$key> is present in 2314%hash, C<exists($hash{$key})> will return true. The value 2315for a given key can be C<undef>, in which case 2316C<$hash{$key}> will be C<undef> while C<exists $hash{$key}> 2317will return true. This corresponds to (C<$key>, C<undef>) 2318being in the hash. 2319 2320Pictures help... Here's the C<%hash> table: 2321 2322 keys values 2323 +------+------+ 2324 | a | 3 | 2325 | x | 7 | 2326 | d | 0 | 2327 | e | 2 | 2328 +------+------+ 2329 2330And these conditions hold 2331 2332 $hash{'a'} is true 2333 $hash{'d'} is false 2334 defined $hash{'d'} is true 2335 defined $hash{'a'} is true 2336 exists $hash{'a'} is true (Perl 5 only) 2337 grep ($_ eq 'a', keys %hash) is true 2338 2339If you now say 2340 2341 undef $hash{'a'} 2342 2343your table now reads: 2344 2345 2346 keys values 2347 +------+------+ 2348 | a | undef| 2349 | x | 7 | 2350 | d | 0 | 2351 | e | 2 | 2352 +------+------+ 2353 2354and these conditions now hold; changes in caps: 2355 2356 $hash{'a'} is FALSE 2357 $hash{'d'} is false 2358 defined $hash{'d'} is true 2359 defined $hash{'a'} is FALSE 2360 exists $hash{'a'} is true (Perl 5 only) 2361 grep ($_ eq 'a', keys %hash) is true 2362 2363Notice the last two: you have an undef value, but a defined key! 2364 2365Now, consider this: 2366 2367 delete $hash{'a'} 2368 2369your table now reads: 2370 2371 keys values 2372 +------+------+ 2373 | x | 7 | 2374 | d | 0 | 2375 | e | 2 | 2376 +------+------+ 2377 2378and these conditions now hold; changes in caps: 2379 2380 $hash{'a'} is false 2381 $hash{'d'} is false 2382 defined $hash{'d'} is true 2383 defined $hash{'a'} is false 2384 exists $hash{'a'} is FALSE (Perl 5 only) 2385 grep ($_ eq 'a', keys %hash) is FALSE 2386 2387See, the whole entry is gone! 2388 2389=head2 Why don't my tied hashes make the defined/exists distinction? 2390 2391This depends on the tied hash's implementation of EXISTS(). 2392For example, there isn't the concept of undef with hashes 2393that are tied to DBM* files. It also means that exists() and 2394defined() do the same thing with a DBM* file, and what they 2395end up doing is not what they do with ordinary hashes. 2396 2397=head2 How do I reset an each() operation part-way through? 2398 2399(contributed by brian d foy) 2400 2401You can use the C<keys> or C<values> functions to reset C<each>. To 2402simply reset the iterator used by C<each> without doing anything else, 2403use one of them in void context: 2404 2405 keys %hash; # resets iterator, nothing else. 2406 values %hash; # resets iterator, nothing else. 2407 2408See the documentation for C<each> in L<perlfunc>. 2409 2410=head2 How can I get the unique keys from two hashes? 2411 2412First you extract the keys from the hashes into lists, then solve 2413the "removing duplicates" problem described above. For example: 2414 2415 my %seen = (); 2416 for my $element (keys(%foo), keys(%bar)) { 2417 $seen{$element}++; 2418 } 2419 my @uniq = keys %seen; 2420 2421Or more succinctly: 2422 2423 my @uniq = keys %{{%foo,%bar}}; 2424 2425Or if you really want to save space: 2426 2427 my %seen = (); 2428 while (defined ($key = each %foo)) { 2429 $seen{$key}++; 2430 } 2431 while (defined ($key = each %bar)) { 2432 $seen{$key}++; 2433 } 2434 my @uniq = keys %seen; 2435 2436=head2 How can I store a multidimensional array in a DBM file? 2437 2438Either stringify the structure yourself (no fun), or else 2439get the MLDBM (which uses Data::Dumper) module from CPAN and layer 2440it on top of either DB_File or GDBM_File. You might also try DBM::Deep, but 2441it can be a bit slow. 2442 2443=head2 How can I make my hash remember the order I put elements into it? 2444 2445Use the L<Tie::IxHash> from CPAN. 2446 2447 use Tie::IxHash; 2448 2449 tie my %myhash, 'Tie::IxHash'; 2450 2451 for (my $i=0; $i<20; $i++) { 2452 $myhash{$i} = 2*$i; 2453 } 2454 2455 my @keys = keys %myhash; 2456 # @keys = (0,1,2,3,...) 2457 2458=head2 Why does passing a subroutine an undefined element in a hash create it? 2459 2460(contributed by brian d foy) 2461 2462Are you using a really old version of Perl? 2463 2464Normally, accessing a hash key's value for a nonexistent key will 2465I<not> create the key. 2466 2467 my %hash = (); 2468 my $value = $hash{ 'foo' }; 2469 print "This won't print\n" if exists $hash{ 'foo' }; 2470 2471Passing C<$hash{ 'foo' }> to a subroutine used to be a special case, though. 2472Since you could assign directly to C<$_[0]>, Perl had to be ready to 2473make that assignment so it created the hash key ahead of time: 2474 2475 my_sub( $hash{ 'foo' } ); 2476 print "This will print before 5.004\n" if exists $hash{ 'foo' }; 2477 2478 sub my_sub { 2479 # $_[0] = 'bar'; # create hash key in case you do this 2480 1; 2481 } 2482 2483Since Perl 5.004, however, this situation is a special case and Perl 2484creates the hash key only when you make the assignment: 2485 2486 my_sub( $hash{ 'foo' } ); 2487 print "This will print, even after 5.004\n" if exists $hash{ 'foo' }; 2488 2489 sub my_sub { 2490 $_[0] = 'bar'; 2491 } 2492 2493However, if you want the old behavior (and think carefully about that 2494because it's a weird side effect), you can pass a hash slice instead. 2495Perl 5.004 didn't make this a special case: 2496 2497 my_sub( @hash{ qw/foo/ } ); 2498 2499=head2 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? 2500 2501Usually a hash ref, perhaps like this: 2502 2503 $record = { 2504 NAME => "Jason", 2505 EMPNO => 132, 2506 TITLE => "deputy peon", 2507 AGE => 23, 2508 SALARY => 37_000, 2509 PALS => [ "Norbert", "Rhys", "Phineas"], 2510 }; 2511 2512References are documented in L<perlref> and L<perlreftut>. 2513Examples of complex data structures are given in L<perldsc> and 2514L<perllol>. Examples of structures and object-oriented classes are 2515in L<perlootut>. 2516 2517=head2 How can I use a reference as a hash key? 2518 2519(contributed by brian d foy and Ben Morrow) 2520 2521Hash keys are strings, so you can't really use a reference as the key. 2522When you try to do that, perl turns the reference into its stringified 2523form (for instance, C<HASH(0xDEADBEEF)>). From there you can't get 2524back the reference from the stringified form, at least without doing 2525some extra work on your own. 2526 2527Remember that the entry in the hash will still be there even if 2528the referenced variable goes out of scope, and that it is entirely 2529possible for Perl to subsequently allocate a different variable at 2530the same address. This will mean a new variable might accidentally 2531be associated with the value for an old. 2532 2533If you have Perl 5.10 or later, and you just want to store a value 2534against the reference for lookup later, you can use the core 2535Hash::Util::Fieldhash module. This will also handle renaming the 2536keys if you use multiple threads (which causes all variables to be 2537reallocated at new addresses, changing their stringification), and 2538garbage-collecting the entries when the referenced variable goes out 2539of scope. 2540 2541If you actually need to be able to get a real reference back from 2542each hash entry, you can use the Tie::RefHash module, which does the 2543required work for you. 2544 2545=head2 How can I check if a key exists in a multilevel hash? 2546 2547(contributed by brian d foy) 2548 2549The trick to this problem is avoiding accidental autovivification. If 2550you want to check three keys deep, you might naE<0xEF>vely try this: 2551 2552 my %hash; 2553 if( exists $hash{key1}{key2}{key3} ) { 2554 ...; 2555 } 2556 2557Even though you started with a completely empty hash, after that call to 2558C<exists> you've created the structure you needed to check for C<key3>: 2559 2560 %hash = ( 2561 'key1' => { 2562 'key2' => {} 2563 } 2564 ); 2565 2566That's autovivification. You can get around this in a few ways. The 2567easiest way is to just turn it off. The lexical C<autovivification> 2568pragma is available on CPAN. Now you don't add to the hash: 2569 2570 { 2571 no autovivification; 2572 my %hash; 2573 if( exists $hash{key1}{key2}{key3} ) { 2574 ...; 2575 } 2576 } 2577 2578The L<Data::Diver> module on CPAN can do it for you too. Its C<Dive> 2579subroutine can tell you not only if the keys exist but also get the 2580value: 2581 2582 use Data::Diver qw(Dive); 2583 2584 my @exists = Dive( \%hash, qw(key1 key2 key3) ); 2585 if( ! @exists ) { 2586 ...; # keys do not exist 2587 } 2588 elsif( ! defined $exists[0] ) { 2589 ...; # keys exist but value is undef 2590 } 2591 2592You can easily do this yourself too by checking each level of the hash 2593before you move onto the next level. This is essentially what 2594L<Data::Diver> does for you: 2595 2596 if( check_hash( \%hash, qw(key1 key2 key3) ) ) { 2597 ...; 2598 } 2599 2600 sub check_hash { 2601 my( $hash, @keys ) = @_; 2602 2603 return unless @keys; 2604 2605 foreach my $key ( @keys ) { 2606 return unless eval { exists $hash->{$key} }; 2607 $hash = $hash->{$key}; 2608 } 2609 2610 return 1; 2611 } 2612 2613=head2 How can I prevent addition of unwanted keys into a hash? 2614 2615Since version 5.8.0, hashes can be I<restricted> to a fixed number 2616of given keys. Methods for creating and dealing with restricted hashes 2617are exported by the L<Hash::Util> module. 2618 2619=head1 Data: Misc 2620 2621=head2 How do I handle binary data correctly? 2622 2623Perl is binary-clean, so it can handle binary data just fine. 2624On Windows or DOS, however, you have to use C<binmode> for binary 2625files to avoid conversions for line endings. In general, you should 2626use C<binmode> any time you want to work with binary data. 2627 2628Also see L<perlfunc/"binmode"> or L<perlopentut>. 2629 2630If you're concerned about 8-bit textual data then see L<perllocale>. 2631If you want to deal with multibyte characters, however, there are 2632some gotchas. See the section on Regular Expressions. 2633 2634=head2 How do I determine whether a scalar is a number/whole/integer/float? 2635 2636Assuming that you don't care about IEEE notations like "NaN" or 2637"Infinity", you probably just want to use a regular expression (see also 2638L<perlretut> and L<perlre>): 2639 2640 use 5.010; 2641 2642 if ( /\D/ ) 2643 { say "\thas nondigits"; } 2644 if ( /^\d+\z/ ) 2645 { say "\tis a whole number"; } 2646 if ( /^-?\d+\z/ ) 2647 { say "\tis an integer"; } 2648 if ( /^[+-]?\d+\z/ ) 2649 { say "\tis a +/- integer"; } 2650 if ( /^-?(?:\d+\.?|\.\d)\d*\z/ ) 2651 { say "\tis a real number"; } 2652 if ( /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i ) 2653 { say "\tis a C float" } 2654 2655There are also some commonly used modules for the task. 2656L<Scalar::Util> (distributed with 5.8) provides access to perl's 2657internal function C<looks_like_number> for determining whether a 2658variable looks like a number. L<Data::Types> exports functions that 2659validate data types using both the above and other regular 2660expressions. Thirdly, there is L<Regexp::Common> which has regular 2661expressions to match various types of numbers. Those three modules are 2662available from the CPAN. 2663 2664If you're on a POSIX system, Perl supports the C<POSIX::strtod> 2665function for converting strings to doubles (and also C<POSIX::strtol> 2666for longs). Its semantics are somewhat cumbersome, so here's a 2667C<getnum> wrapper function for more convenient access. This function 2668takes a string and returns the number it found, or C<undef> for input 2669that isn't a C float. The C<is_numeric> function is a front end to 2670C<getnum> if you just want to say, "Is this a float?" 2671 2672 sub getnum { 2673 use POSIX qw(strtod); 2674 my $str = shift; 2675 $str =~ s/^\s+//; 2676 $str =~ s/\s+$//; 2677 $! = 0; 2678 my($num, $unparsed) = strtod($str); 2679 if (($str eq '') || ($unparsed != 0) || $!) { 2680 return undef; 2681 } 2682 else { 2683 return $num; 2684 } 2685 } 2686 2687 sub is_numeric { defined getnum($_[0]) } 2688 2689Or you could check out the L<String::Scanf> module on the CPAN 2690instead. 2691 2692=head2 How do I keep persistent data across program calls? 2693 2694For some specific applications, you can use one of the DBM modules. 2695See L<AnyDBM_File>. More generically, you should consult the L<FreezeThaw> 2696or L<Storable> modules from CPAN. Starting from Perl 5.8, L<Storable> is part 2697of the standard distribution. Here's one example using L<Storable>'s C<store> 2698and C<retrieve> functions: 2699 2700 use Storable; 2701 store(\%hash, "filename"); 2702 2703 # later on... 2704 $href = retrieve("filename"); # by ref 2705 %hash = %{ retrieve("filename") }; # direct to hash 2706 2707=head2 How do I print out or copy a recursive data structure? 2708 2709The L<Data::Dumper> module on CPAN (or the 5.005 release of Perl) is great 2710for printing out data structures. The L<Storable> module on CPAN (or the 27115.8 release of Perl), provides a function called C<dclone> that recursively 2712copies its argument. 2713 2714 use Storable qw(dclone); 2715 $r2 = dclone($r1); 2716 2717Where C<$r1> can be a reference to any kind of data structure you'd like. 2718It will be deeply copied. Because C<dclone> takes and returns references, 2719you'd have to add extra punctuation if you had a hash of arrays that 2720you wanted to copy. 2721 2722 %newhash = %{ dclone(\%oldhash) }; 2723 2724=head2 How do I define methods for every class/object? 2725 2726(contributed by Ben Morrow) 2727 2728You can use the C<UNIVERSAL> class (see L<UNIVERSAL>). However, please 2729be very careful to consider the consequences of doing this: adding 2730methods to every object is very likely to have unintended 2731consequences. If possible, it would be better to have all your object 2732inherit from some common base class, or to use an object system like 2733Moose that supports roles. 2734 2735=head2 How do I verify a credit card checksum? 2736 2737Get the L<Business::CreditCard> module from CPAN. 2738 2739=head2 How do I pack arrays of doubles or floats for XS code? 2740 2741The arrays.h/arrays.c code in the L<PGPLOT> module on CPAN does just this. 2742If you're doing a lot of float or double processing, consider using 2743the L<PDL> module from CPAN instead--it makes number-crunching easy. 2744 2745See L<https://metacpan.org/release/PGPLOT> for the code. 2746 2747 2748=head1 AUTHOR AND COPYRIGHT 2749 2750Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and 2751other authors as noted. All rights reserved. 2752 2753This documentation is free; you can redistribute it and/or modify it 2754under the same terms as Perl itself. 2755 2756Irrespective of its distribution, all code examples in this file 2757are hereby placed into the public domain. You are permitted and 2758encouraged to use this code in your own programs for fun 2759or for profit as you see fit. A simple comment in the code giving 2760credit would be courteous but is not required. 2761