3 perlfaq4 - Data Manipulation ($Revision: 1.19 $, $Date: 1997/04/24 22:43:57 $)
7 The section of the FAQ answers question related to the manipulation
8 of data as numbers, dates, strings, arrays, hashes, and miscellaneous
13 =head2 Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
15 Internally, your computer represents floating-point numbers in binary.
16 Floating-point numbers read in from a file, or appearing as literals
17 in your program, are converted from their decimal floating-point
18 representation (eg, 19.95) to the internal binary representation.
20 However, 19.95 can't be precisely represented as a binary
21 floating-point number, just like 1/3 can't be exactly represented as a
22 decimal floating-point number. The computer's binary representation
23 of 19.95, therefore, isn't exactly 19.95.
25 When a floating-point number gets printed, the binary floating-point
26 representation is converted back to decimal. These decimal numbers
27 are displayed in either the format you specify with printf(), or the
28 current output format for numbers (see L<perlvar/"$#"> if you use
29 print. C<$#> has a different default value in Perl5 than it did in
30 Perl4. Changing C<$#> yourself is deprecated.
32 This affects B<all> computer languages that represent decimal
33 floating-point numbers in binary, not just Perl. Perl provides
34 arbitrary-precision decimal numbers with the Math::BigFloat module
35 (part of the standard Perl distribution), but mathematical operations
36 are consequently slower.
38 To get rid of the superfluous digits, just use a format (eg,
39 C<printf("%.2f", 19.95)>) to get the required precision.
41 =head2 Why isn't my octal data interpreted correctly?
43 Perl only understands octal and hex numbers as such when they occur
44 as literals in your program. If they are read in from somewhere and
45 assigned, no automatic conversion takes place. You must explicitly
46 use oct() or hex() if you want the values converted. oct() interprets
47 both hex ("0x350") numbers and octal ones ("0350" or even without the
48 leading "0", like "377"), while hex() only converts hexadecimal ones,
49 with or without a leading "0x", like "0x255", "3A", "ff", or "deadbeef".
51 This problem shows up most often when people try using chmod(), mkdir(),
52 umask(), or sysopen(), which all want permissions in octal.
54 chmod(644, $file); # WRONG -- perl -w catches this
55 chmod(0644, $file); # right
57 =head2 Does perl have a round function? What about ceil() and floor()?
60 For rounding to a certain number of digits, sprintf() or printf() is
61 usually the easiest route.
63 The POSIX module (part of the standard perl distribution) implements
64 ceil(), floor(), and a number of other mathematical and trigonometric
67 In 5.000 to 5.003 Perls, trigonometry was done in the Math::Complex
68 module. With 5.004, the Math::Trig module (part of the standard perl
69 distribution) implements the trigonometric functions. Internally it
70 uses the Math::Complex module and some functions can break out from
71 the real axis into the complex plane, for example the inverse sine of
74 Rounding in financial applications can have serious implications, and
75 the rounding method used should be specified precisely. In these
76 cases, it probably pays not to trust whichever system rounding is
77 being used by Perl, but to instead implement the rounding function you
80 =head2 How do I convert bits into ints?
82 To turn a string of 1s and 0s like '10110110' into a scalar containing
83 its binary value, use the pack() function (documented in
86 $decimal = pack('B8', '10110110');
88 Here's an example of going the other way:
90 $binary_string = join('', unpack('B*', "\x29"));
92 =head2 How do I multiply matrices?
94 Use the Math::Matrix or Math::MatrixReal modules (available from CPAN)
95 or the PDL extension (also available from CPAN).
97 =head2 How do I perform an operation on a series of integers?
99 To call a function on each element in an array, and collect the
102 @results = map { my_func($_) } @array;
106 @triple = map { 3 * $_ } @single;
108 To call a function on each element of an array, but ignore the
111 foreach $iterator (@array) {
115 To call a function on each integer in a (small) range, you B<can> use:
117 @results = map { &my_func($_) } (5 .. 25);
119 but you should be aware that the C<..> operator creates an array of
120 all integers in the range. This can take a lot of memory for large
124 for ($i=5; $i < 500_005; $i++) {
125 push(@results, &my_func($i));
128 =head2 How can I output Roman numerals?
130 Get the http://www.perl.com/CPAN/modules/by-module/Roman module.
132 =head2 Why aren't my random numbers random?
134 The short explanation is that you're getting pseudorandom numbers, not
135 random ones, because that's how these things work. A longer
136 explanation is available on
137 http://www.perl.com/CPAN/doc/FMTEYEWTK/random, courtesy of Tom
140 You should also check out the Math::TrulyRandom module from CPAN.
144 =head2 How do I find the week-of-the-year/day-of-the-year?
146 The day of the year is in the array returned by localtime() (see
147 L<perlfunc/"localtime">):
149 $day_of_year = (localtime(time()))[7];
151 or more legibly (in 5.004 or higher):
154 $day_of_year = localtime(time())->yday;
156 You can find the week of the year by dividing this by 7:
158 $week_of_year = int($day_of_year / 7);
160 Of course, this believes that weeks start at zero.
162 =head2 How can I compare two date strings?
164 Use the Date::Manip or Date::DateCalc modules from CPAN.
166 =head2 How can I take a string and turn it into epoch seconds?
168 If it's a regular enough string that it always has the same format,
169 you can split it up and pass the parts to timelocal in the standard
170 Time::Local module. Otherwise, you should look into one of the
171 Date modules from CPAN.
173 =head2 How can I find the Julian Day?
175 Neither Date::Manip nor Date::DateCalc deal with Julian days.
176 Instead, there is an example of Julian date calculation in
177 http://www.perl.com/CPAN/authors/David_Muir_Sharnoff/modules/Time/JulianDay.pm.gz,
180 =head2 Does Perl have a year 2000 problem?
182 Not unless you use Perl to create one. The date and time functions
183 supplied with perl (gmtime and localtime) supply adequate information
184 to determine the year well beyond 2000 (2038 is when trouble strikes).
185 The year returned by these functions when used in an array context is
186 the year minus 1900. For years between 1910 and 1999 this I<happens>
187 to be a 2-digit decimal number. To avoid the year 2000 problem simply
188 do not treat the year as a 2-digit number. It isn't.
190 When gmtime() and localtime() are used in a scalar context they return
191 a timestamp string that contains a fully-expanded year. For example,
192 C<$timestamp = gmtime(1005613200)> sets $timestamp to "Tue Nov 13 01:00:00
193 2001". There's no year 2000 problem here.
197 =head2 How do I validate input?
199 The answer to this question is usually a regular expression, perhaps
200 with auxiliary logic. See the more specific questions (numbers, email
201 addresses, etc.) for details.
203 =head2 How do I unescape a string?
205 It depends just what you mean by "escape". URL escapes are dealt with
206 in L<perlfaq9>. Shell escapes with the backslash (\)
207 character are removed with:
211 Note that this won't expand \n or \t or any other special escapes.
213 =head2 How do I remove consecutive pairs of characters?
215 To turn "abbcccd" into "abccd":
219 =head2 How do I expand function calls in a string?
221 This is documented in L<perlref>. In general, this is fraught with
222 quoting and readability problems, but it is possible. To interpolate
223 a subroutine call (in a list context) into a string:
225 print "My sub returned @{[mysub(1,2,3)]} that time.\n";
227 If you prefer scalar context, similar chicanery is also useful for
228 arbitrary expressions:
230 print "That yields ${\($n + 5)} widgets\n";
232 See also "How can I expand variables in text strings?" in this section
235 =head2 How do I find matching/nesting anything?
237 This isn't something that can be tackled in one regular expression, no
238 matter how complicated. To find something between two single characters,
239 a pattern like C</x([^x]*)x/> will get the intervening bits in $1. For
240 multiple ones, then something more like C</alpha(.*?)omega/> would
241 be needed. But none of these deals with nested patterns, nor can they.
242 For that you'll have to write a parser.
244 =head2 How do I reverse a string?
246 Use reverse() in a scalar context, as documented in
249 $reversed = reverse $string;
251 =head2 How do I expand tabs in a string?
253 You can do it the old-fashioned way:
255 1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
257 Or you can just use the Text::Tabs module (part of the standard perl
261 @expanded_lines = expand(@lines_with_tabs);
263 =head2 How do I reformat a paragraph?
265 Use Text::Wrap (part of the standard perl distribution):
268 print wrap("\t", ' ', @paragraphs);
270 The paragraphs you give to Text::Wrap may not contain embedded
271 newlines. Text::Wrap doesn't justify the lines (flush-right).
273 =head2 How can I access/change the first N letters of a string?
275 There are many ways. If you just want to grab a copy, use
278 $first_byte = substr($a, 0, 1);
280 If you want to modify part of a string, the simplest way is often to
281 use substr() as an lvalue:
283 substr($a, 0, 3) = "Tom";
285 Although those with a regexp kind of thought process will likely prefer
289 =head2 How do I change the Nth occurrence of something?
291 You have to keep track. For example, let's say you want
292 to change the fifth occurrence of "whoever" or "whomever"
293 into "whosoever" or "whomsoever", case insensitively.
297 ++$count == 5 # is it the 5th?
298 ? "${2}soever" # yes, swap
299 : $1 # renege and leave it there
302 =head2 How can I count the number of occurrences of a substring within a string?
304 There are a number of ways, with varying efficiency: If you want a
305 count of a certain single character (X) within a string, you can use the
306 C<tr///> function like so:
308 $string = "ThisXlineXhasXsomeXx'sXinXit":
309 $count = ($string =~ tr/X//);
310 print "There are $count X charcters in the string";
312 This is fine if you are just looking for a single character. However,
313 if you are trying to count multiple character substrings within a
314 larger string, C<tr///> won't work. What you can do is wrap a while()
315 loop around a global pattern match. For example, let's count negative
318 $string = "-9 55 48 -2 23 -76 4 14 -44";
319 while ($string =~ /-\d+/g) { $count++ }
320 print "There are $count negative numbers in the string";
322 =head2 How do I capitalize all the words on one line?
324 To make the first letter of each word upper case:
326 $line =~ s/\b(\w)/\U$1/g;
328 This has the strange effect of turning "C<don't do it>" into
329 "C<Don'T Do It>". Sometimes you might want this, instead
330 (Suggested by Brian Foy E<lt>comdog@computerdog.comE<gt>):
333 (^\w) #at the beginning of the line
335 (\s\w) #preceded by whitespace
338 $string =~ /([\w']+)/\u\L$1/g;
340 To make the whole line upper case:
344 To force each word to be lower case, with the first letter upper case:
346 $line =~ s/(\w+)/\u\L$1/g;
348 =head2 How can I split a [character] delimited string except when inside
349 [character]? (Comma-separated files)
351 Take the example case of trying to split a string that is comma-separated
352 into its different fields. (We'll pretend you said comma-separated, not
353 comma-delimited, which is different and almost never what you mean.) You
354 can't use C<split(/,/)> because you shouldn't split if the comma is inside
355 quotes. For example, take a data line like this:
357 SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Error, Core Dumped"
359 Due to the restriction of the quotes, this is a fairly complex
360 problem. Thankfully, we have Jeffrey Friedl, author of a highly
361 recommended book on regular expressions, to handle these for us. He
362 suggests (assuming your string is contained in $text):
365 push(@new, $+) while $text =~ m{
366 "([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes
370 push(@new, undef) if substr($text,-1,1) eq ',';
372 If you want to represent quotation marks inside a
373 quotation-mark-delimited field, escape them with backslashes (eg,
374 C<"like \"this\""). Unescaping them is a task addressed earlier in
377 Alternatively, the Text::ParseWords module (part of the standard perl
378 distribution) lets you say:
380 use Text::ParseWords;
381 @new = quotewords(",", 0, $text);
383 =head2 How do I strip blank space from the beginning/end of a string?
385 The simplest approach, albeit not the fastest, is probably like this:
387 $string =~ s/^\s*(.*?)\s*$/$1/;
389 It would be faster to do this in two steps:
394 Or more nicely written as:
401 =head2 How do I extract selected columns from a string?
403 Use substr() or unpack(), both documented in L<perlfunc>.
405 =head2 How do I find the soundex value of a string?
407 Use the standard Text::Soundex module distributed with perl.
409 =head2 How can I expand variables in text strings?
411 Let's assume that you have a string like:
413 $text = 'this has a $foo in it and a $bar';
414 $text =~ s/\$(\w+)/${$1}/g;
416 Before version 5 of perl, this had to be done with a double-eval
419 $text =~ s/(\$\w+)/$1/eeg;
421 Which is bizarre enough that you'll probably actually need an EEG
424 See also "How do I expand function calls in a string?" in this section
427 =head2 What's wrong with always quoting "$vars"?
429 The problem is that those double-quotes force stringification,
430 coercing numbers and references into strings, even when you
431 don't want them to be.
433 If you get used to writing odd things like these:
437 somefunc("$var"); # BAD
439 You'll be in trouble. Those should (in 99.8% of the cases) be
440 the simpler and more direct:
446 Otherwise, besides slowing you down, you're going to break code when
447 the thing in the scalar is actually neither a string nor a number, but
453 my $oref = "$aref"; # WRONG
456 You can also get into subtle problems on those few operations in Perl
457 that actually do care about the difference between a string and a
458 number, such as the magical C<++> autoincrement operator or the
461 =head2 Why don't my <<HERE documents work?
463 Check for these three things:
467 =item 1. There must be no space after the << part.
469 =item 2. There (probably) should be a semicolon at the end.
471 =item 3. You can't (easily) have any space in front of the tag.
477 =head2 What is the difference between $array[1] and @array[1]?
479 The former is a scalar value, the latter an array slice, which makes
480 it a list with one (scalar) value. You should use $ when you want a
481 scalar value (most of the time) and @ when you want a list with one
482 scalar value in it (very, very rarely; nearly never, in fact).
484 Sometimes it doesn't make a difference, but sometimes it does.
485 For example, compare:
487 $good[0] = `some program that outputs several lines`;
491 @bad[0] = `same program that outputs several lines`;
493 The B<-w> flag will warn you about these matters.
495 =head2 How can I extract just the unique elements of an array?
497 There are several possible ways, depending on whether the array is
498 ordered and whether you wish to preserve the ordering.
502 =item a) If @in is sorted, and you want @out to be sorted:
505 @out = grep($_ ne $prev && ($prev = $_), @in);
507 This is nice in that it doesn't use much extra memory,
508 simulating uniq(1)'s behavior of removing only adjacent
511 =item b) If you don't know whether @in is sorted:
514 @out = grep(!$saw{$_}++, @in);
516 =item c) Like (b), but @in contains only small integers:
518 @out = grep(!$saw[$_]++, @in);
520 =item d) A way to do (b) without any loops or greps:
524 @out = sort keys %saw; # remove sort if undesired
526 =item e) Like (d), but @in contains only small positive integers:
534 =head2 How can I tell whether an array contains a certain element?
536 There are several ways to approach this. If you are going to make
537 this query many times and the values are arbitrary strings, the
538 fastest way is probably to invert the original array and keep an
539 associative array lying about whose keys are the first array's values.
541 @blues = qw/azure cerulean teal turquoise lapis-lazuli/;
543 for (@blues) { $is_blue{$_} = 1 }
545 Now you can check whether $is_blue{$some_color}. It might have been a
546 good idea to keep the blues all in a hash in the first place.
548 If the values are all small integers, you could use a simple indexed
549 array. This kind of an array will take up less space:
551 @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
552 undef @is_tiny_prime;
553 for (@primes) { $is_tiny_prime[$_] = 1; }
555 Now you check whether $is_tiny_prime[$some_number].
557 If the values in question are integers instead of strings, you can save
558 quite a lot of space by using bit strings instead:
560 @articles = ( 1..10, 150..2000, 2017 );
562 grep (vec($read,$_,1) = 1, @articles);
564 Now check whether C<vec($read,$n,1)> is true for some C<$n>.
568 $is_there = grep $_ eq $whatever, @array;
572 $is_there = grep /$whatever/, @array;
574 These are slow (checks every element even if the first matches),
575 inefficient (same reason), and potentially buggy (what if there are
576 regexp characters in $whatever?).
578 =head2 How do I compute the difference of two arrays? How do I compute the intersection of two arrays?
580 Use a hash. Here's code to do both and more. It assumes that
581 each element is unique in a given array:
583 @union = @intersection = @difference = ();
585 foreach $element (@array1, @array2) { $count{$element}++ }
586 foreach $element (keys %count) {
587 push @union, $element;
588 push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
591 =head2 How do I find the first array element for which a condition is true?
593 You can use this if you care about the index:
595 for ($i=0; $i < @array; $i++) {
596 if ($array[$i] eq "Waldo") {
602 Now C<$found_index> has what you want.
604 =head2 How do I handle linked lists?
606 In general, you usually don't need a linked list in Perl, since with
607 regular arrays, you can push and pop or shift and unshift at either end,
608 or you can use splice to add and/or remove arbitrary number of elements
611 If you really, really wanted, you could use structures as described in
612 L<perldsc> or L<perltoot> and do just what the algorithm book tells you
615 =head2 How do I handle circular lists?
617 Circular lists could be handled in the traditional fashion with linked
618 lists, or you could just do something like this with an array:
620 unshift(@array, pop(@array)); # the last shall be first
621 push(@array, shift(@array)); # and vice versa
623 =head2 How do I shuffle an array randomly?
625 Here's a shuffling algorithm which works its way through the list,
626 randomly picking another element to swap the current element with:
630 @old = 1 .. 10; # just a demo
632 push(@new, splice(@old, rand @old, 1));
635 For large arrays, this avoids a lot of the reshuffling:
639 @old = 1 .. 10000; # just a demo
646 =head2 How do I process/modify each element of an array?
648 Use C<for>/C<foreach>:
655 Here's another; let's compute spherical volumes:
659 $_ *= (4/3) * 3.14159; # this will be constant folded
662 =head2 How do I select a random element from an array?
664 Use the rand() function (see L<perlfunc/rand>):
666 srand; # not needed for 5.004 and later
667 $index = rand @array;
668 $element = $array[$index];
670 =head2 How do I permute N elements of a list?
672 Here's a little program that generates all permutations
673 of all the words on each line of input. The algorithm embodied
674 in the permut() function should work on any list:
677 # permute - tchrist@perl.com
680 my @head = @{ $_[0] };
681 my @tail = @{ $_[1] };
683 # stop recursing when there are no elements in the head
686 # for all elements in @head, move one from @head to @tail
687 # and call permut() on the new @head and @tail
688 my(@newhead,@newtail,$i);
689 foreach $i (0 .. $#head) {
692 unshift(@newtail, splice(@newhead, $i, 1));
693 permut([@newhead], [@newtail]);
698 =head2 How do I sort an array by (anything)?
700 Supply a comparison function to sort() (described in L<perlfunc/sort>):
702 @list = sort { $a <=> $b } @list;
704 The default sort function is cmp, string comparison, which would
705 sort C<(1, 2, 10)> into C<(1, 10, 2)>. C<E<lt>=E<gt>>, used above, is
706 the numerical comparison operator.
708 If you have a complicated function needed to pull out the part you
709 want to sort on, then don't do it inside the sort function. Pull it
710 out first, because the sort BLOCK can be called many times for the
711 same element. Here's an example of how to pull out the first word
712 after the first number on each item, and then sort those words
717 ($item) = /\d+\s*(\S+)/;
718 push @idx, uc($item);
720 @sorted = @data[ sort { $idx[$a] cmp $idx[$b] } 0 .. $#idx ];
722 Which could also be written this way, using a trick
723 that's come to be known as the Schwartzian Transform:
725 @sorted = map { $_->[0] }
726 sort { $a->[1] cmp $b->[1] }
727 map { [ $_, uc((/\d+\s*(\S+)/ )[0] ] } @data;
729 If you need to sort on several fields, the following paradigm is useful.
731 @sorted = sort { field1($a) <=> field1($b) ||
732 field2($a) cmp field2($b) ||
733 field3($a) cmp field3($b)
736 This can be conveniently combined with precalculation of keys as given
739 See http://www.perl.com/CPAN/doc/FMTEYEWTK/sort.html for more about
742 See also the question below on sorting hashes.
744 =head2 How do I manipulate arrays of bits?
746 Use pack() and unpack(), or else vec() and the bitwise operations.
748 For example, this sets $vec to have bit N set if $ints[N] was set:
751 foreach(@ints) { vec($vec,$_,1) = 1 }
753 And here's how, given a vector in $vec, you can
754 get those bits into your @ints array:
759 # Find null-byte density then select best algorithm
760 if ($vec =~ tr/\0// / length $vec > 0.95) {
763 # This method is faster with mostly null-bytes
764 while($vec =~ /[^\0]/g ) {
765 $i = -9 + 8 * pos $vec;
766 push @ints, $i if vec($vec, ++$i, 1);
767 push @ints, $i if vec($vec, ++$i, 1);
768 push @ints, $i if vec($vec, ++$i, 1);
769 push @ints, $i if vec($vec, ++$i, 1);
770 push @ints, $i if vec($vec, ++$i, 1);
771 push @ints, $i if vec($vec, ++$i, 1);
772 push @ints, $i if vec($vec, ++$i, 1);
773 push @ints, $i if vec($vec, ++$i, 1);
776 # This method is a fast general algorithm
778 my $bits = unpack "b*", $vec;
779 push @ints, 0 if $bits =~ s/^(\d)// && $1;
780 push @ints, pos $bits while($bits =~ /1/g);
785 This method gets faster the more sparse the bit vector is.
786 (Courtesy of Tim Bunce and Winfried Koenig.)
788 =head2 Why does defined() return true on empty arrays and hashes?
790 See L<perlfunc/defined> in the 5.004 release or later of Perl.
792 =head1 Data: Hashes (Associative Arrays)
794 =head2 How do I process an entire hash?
796 Use the each() function (see L<perlfunc/each>) if you don't care
799 while (($key,$value) = each %hash) {
800 print "$key = $value\n";
803 If you want it sorted, you'll have to use foreach() on the result of
804 sorting the keys as shown in an earlier question.
806 =head2 What happens if I add or remove keys from a hash while iterating over it?
810 =head2 How do I look up a hash element by value?
812 Create a reverse hash:
814 %by_value = reverse %by_key;
815 $key = $by_value{$value};
817 That's not particularly efficient. It would be more space-efficient
820 while (($key, $value) = each %by_key) {
821 $by_value{$value} = $key;
824 If your hash could have repeated values, the methods above will only
825 find one of the associated keys. This may or may not worry you.
827 =head2 How can I know how many entries are in a hash?
829 If you mean how many keys, then all you have to do is
830 take the scalar sense of the keys() function:
832 $num_keys = scalar keys %hash;
834 In void context it just resets the iterator, which is faster
837 =head2 How do I sort a hash (optionally by value instead of key)?
839 Internally, hashes are stored in a way that prevents you from imposing
840 an order on key-value pairs. Instead, you have to sort a list of the
843 @keys = sort keys %hash; # sorted by key
845 $hash{$a} cmp $hash{$b}
846 } keys %hash; # and by value
848 Here we'll do a reverse numeric sort by value, and if two keys are
849 identical, sort by length of key, and if that fails, by straight ASCII
850 comparison of the keys (well, possibly modified by your locale -- see
854 $hash{$b} <=> $hash{$a}
856 length($b) <=> length($a)
861 =head2 How can I always keep my hash sorted?
863 You can look into using the DB_File module and tie() using the
864 $DB_BTREE hash bindings as documented in L<DB_File/"In Memory Databases">.
866 =head2 What's the difference between "delete" and "undef" with hashes?
868 Hashes are pairs of scalars: the first is the key, the second is the
869 value. The key will be coerced to a string, although the value can be
870 any kind of scalar: string, number, or reference. If a key C<$key> is
871 present in the array, C<exists($key)> will return true. The value for
872 a given key can be C<undef>, in which case C<$array{$key}> will be
873 C<undef> while C<$exists{$key}> will return true. This corresponds to
874 (C<$key>, C<undef>) being in the hash.
876 Pictures help... here's the C<%ary> table:
886 And these conditions hold
890 defined $ary{'d'} is true
891 defined $ary{'a'} is true
892 exists $ary{'a'} is true (perl5 only)
893 grep ($_ eq 'a', keys %ary) is true
899 your table now reads:
910 and these conditions now hold; changes in caps:
914 defined $ary{'d'} is true
915 defined $ary{'a'} is FALSE
916 exists $ary{'a'} is true (perl5 only)
917 grep ($_ eq 'a', keys %ary) is true
919 Notice the last two: you have an undef value, but a defined key!
925 your table now reads:
934 and these conditions now hold; changes in caps:
938 defined $ary{'d'} is true
939 defined $ary{'a'} is false
940 exists $ary{'a'} is FALSE (perl5 only)
941 grep ($_ eq 'a', keys %ary) is FALSE
943 See, the whole entry is gone!
945 =head2 Why don't my tied hashes make the defined/exists distinction?
947 They may or may not implement the EXISTS() and DEFINED() methods
948 differently. For example, there isn't the concept of undef with hashes
949 that are tied to DBM* files. This means the true/false tables above
950 will give different results when used on such a hash. It also means
951 that exists and defined do the same thing with a DBM* file, and what
952 they end up doing is not what they do with ordinary hashes.
954 =head2 How do I reset an each() operation part-way through?
956 Using C<keys %hash> in a scalar context returns the number of keys in
957 the hash I<and> resets the iterator associated with the hash. You may
958 need to do this if you use C<last> to exit a loop early so that when you
959 re-enter it, the hash iterator has been reset.
961 =head2 How can I get the unique keys from two hashes?
963 First you extract the keys from the hashes into arrays, and then solve
964 the uniquifying the array problem described above. For example:
967 for $element (keys(%foo), keys(%bar)) {
974 @uniq = keys %{{%foo,%bar}};
976 Or if you really want to save space:
979 while (defined ($key = each %foo)) {
982 while (defined ($key = each %bar)) {
987 =head2 How can I store a multidimensional array in a DBM file?
989 Either stringify the structure yourself (no fun), or else
990 get the MLDBM (which uses Data::Dumper) module from CPAN and layer
991 it on top of either DB_File or GDBM_File.
993 =head2 How can I make my hash remember the order I put elements into it?
995 Use the Tie::IxHash from CPAN.
998 tie(%myhash, Tie::IxHash);
999 for ($i=0; $i<20; $i++) {
1002 @keys = keys %myhash;
1003 # @keys = (0,1,2,3,...)
1005 =head2 Why does passing a subroutine an undefined element in a hash create it?
1007 If you say something like:
1009 somefunc($hash{"nonesuch key here"});
1011 Then that element "autovivifies"; that is, it springs into existence
1012 whether you store something there or not. That's because functions
1013 get scalars passed in by reference. If somefunc() modifies C<$_[0]>,
1014 it has to be ready to write it back into the caller's version.
1016 This has been fixed as of perl5.004.
1018 Normally, merely accessing a key's value for a nonexistent key does
1019 I<not> cause that key to be forever there. This is different than
1022 =head2 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?
1024 Use references (documented in L<perlref>). Examples of complex data
1025 structures are given in L<perldsc> and L<perllol>. Examples of
1026 structures and object-oriented classes are in L<perltoot>.
1028 =head2 How can I use a reference as a hash key?
1030 You can't do this directly, but you could use the standard Tie::Refhash
1031 module distributed with perl.
1035 =head2 How do I handle binary data correctly?
1037 Perl is binary clean, so this shouldn't be a problem. For example,
1038 this works fine (assuming the files are found):
1040 if (`cat /vmunix` =~ /gzip/) {
1041 print "Your kernel is GNU-zip enabled!\n";
1044 On some systems, however, you have to play tedious games with "text"
1045 versus "binary" files. See L<perlfunc/"binmode">.
1047 If you're concerned about 8-bit ASCII data, then see L<perllocale>.
1049 If you want to deal with multibyte characters, however, there are
1050 some gotchas. See the section on Regular Expressions.
1052 =head2 How do I determine whether a scalar is a number/whole/integer/float?
1054 Assuming that you don't care about IEEE notations like "NaN" or
1055 "Infinity", you probably just want to use a regular expression.
1057 warn "has nondigits" if /\D/;
1058 warn "not a whole number" unless /^\d+$/;
1059 warn "not an integer" unless /^-?\d+$/; # reject +3
1060 warn "not an integer" unless /^[+-]?\d+$/;
1061 warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2
1062 warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
1063 warn "not a C float"
1064 unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
1066 Or you could check out
1067 http://www.perl.com/CPAN/modules/by-module/String/String-Scanf-1.1.tar.gz
1068 instead. The POSIX module (part of the standard Perl distribution)
1069 provides the C<strtol> and C<strtod> for converting strings to double
1070 and longs, respectively.
1072 =head2 How do I keep persistent data across program calls?
1074 For some specific applications, you can use one of the DBM modules.
1075 See L<AnyDBM_File>. More generically, you should consult the
1076 FreezeThaw, Storable, or Class::Eroot modules from CPAN.
1078 =head2 How do I print out or copy a recursive data structure?
1080 The Data::Dumper module on CPAN is nice for printing out
1081 data structures, and FreezeThaw for copying them. For example:
1083 use FreezeThaw qw(freeze thaw);
1084 $new = thaw freeze $old;
1086 Where $old can be (a reference to) any kind of data structure you'd like.
1087 It will be deeply copied.
1089 =head2 How do I define methods for every class/object?
1091 Use the UNIVERSAL class (see L<UNIVERSAL>).
1093 =head2 How do I verify a credit card checksum?
1095 Get the Business::CreditCard module from CPAN.
1097 =head1 AUTHOR AND COPYRIGHT
1099 Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
1100 All rights reserved. See L<perlfaq> for distribution information.