=head1 NAME
-perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date: 1999/05/23 20:37:49 $)
+perlfaq4 - Data Manipulation ($Revision: 1.2 $, $Date: 2001/09/26 15:42:12 $)
=head1 DESCRIPTION
(part of the standard Perl distribution), but mathematical operations
are consequently slower.
+If precision is important, such as when dealing with money, it's good
+to work with integers and then divide at the last possible moment.
+For example, work in pennies (1995) instead of dollars and cents
+(19.95) and divide by 100 at the end.
+
To get rid of the superfluous digits, just use a format (eg,
C<printf("%.2f", 19.95)>) to get the required precision.
See L<perlop/"Floating-point Arithmetic">.
=head2 Why isn't my octal data interpreted correctly?
Perl only understands octal and hex numbers as such when they occur
-as literals in your program. If they are read in from somewhere and
-assigned, no automatic conversion takes place. You must explicitly
-use oct() or hex() if you want the values converted. oct() interprets
+as literals in your program. Octal literals in perl must start with
+a leading "0" and hexadecimal literals must start with a leading "0x".
+If they are read in from somewhere and assigned, no automatic
+conversion takes place. You must explicitly use oct() or hex() if you
+want the values converted to decimal. oct() interprets
both hex ("0x350") numbers and octal ones ("0350" or even without the
leading "0", like "377"), while hex() only converts hexadecimal ones,
with or without a leading "0x", like "0x255", "3A", "ff", or "deadbeef".
+The inverse mapping from decimal to octal can be done with either the
+"%o" or "%O" sprintf() formats. To get from decimal to hex try either
+the "%x" or the "%X" formats to sprintf().
This problem shows up most often when people try using chmod(), mkdir(),
-umask(), or sysopen(), which all want permissions in octal.
+umask(), or sysopen(), which by widespread tradition typically take
+permissions in octal.
- chmod(644, $file); # WRONG -- perl -w catches this
+ chmod(644, $file); # WRONG
chmod(0644, $file); # right
+Note the mistake in the first line was specifying the decimal literal
+644, rather than the intended octal literal 0644. The problem can
+be seen with:
+
+ printf("%#o",644); # prints 01204
+
+Surely you had not intended C<chmod(01204, $file);> - did you? If you
+want to use numeric literals as arguments to chmod() et al. then please
+try to express them as octal constants, that is with a leading zero and
+with the following digits restricted to the set 0..7.
+
=head2 Does Perl have a round() function? What about ceil() and floor()? Trig functions?
Remember that int() merely truncates toward 0. For rounding to a
$day_of_year = (localtime(time()))[7];
-or more legibly (in 5.004 or higher):
-
- use Time::localtime;
- $day_of_year = localtime(time())->yday;
-
-You can find the week of the year by dividing this by 7:
-
- $week_of_year = int($day_of_year / 7);
-
-Of course, this believes that weeks start at zero. The Date::Calc
-module from CPAN has a lot of date calculation functions, including
-day of the year, week of the year, and so on. Note that not
-all businesses consider ``week 1'' to be the same; for example,
-American businesses often consider the first week with a Monday
-in it to be Work Week #1, despite ISO 8601, which considers
-WW1 to be the first week with a Thursday in it.
-
=head2 How do I find the current century or millennium?
Use the following simple functions:
Use the Time::JulianDay module (part of the Time-modules bundle
available from CPAN.)
-Before you immerse yourself too deeply in this, be sure to verify that it
-is the I<Julian> Day you really want. Are you really just interested in
-a way of getting serial days so that they can do date arithmetic? If you
+Before you immerse yourself too deeply in this, be sure to verify that
+it is the I<Julian> Day you really want. Are you interested in a way
+of getting serial days so that you just can tell how many days they
+are apart or so that you can do also other date arithmetic? If you
are interested in performing date arithmetic, this can be done using
-either Date::Manip or Date::Calc, without converting to Julian Day first.
-
-There is too much confusion on this issue to cover in this FAQ, but the
-term is applied (correctly) to a calendar now supplanted by the Gregorian
-Calendar, with the Julian Calendar failing to adjust properly for leap
-years on centennial years (among other annoyances). The term is also used
-(incorrectly) to mean: [1] days in the Gregorian Calendar; and [2] days
-since a particular starting time or `epoch', usually 1970 in the Unix
-world and 1980 in the MS-DOS/Windows world. If you find that it is not
-the first meaning that you really want, then check out the Date::Manip
-and Date::Calc modules. (Thanks to David Cassell for most of this text.)
+modules Date::Manip or Date::Calc.
+
+There is too many details and much confusion on this issue to cover in
+this FAQ, but the term is applied (correctly) to a calendar now
+supplanted by the Gregorian Calendar, with the Julian Calendar failing
+to adjust properly for leap years on centennial years (among other
+annoyances). The term is also used (incorrectly) to mean: [1] days in
+the Gregorian Calendar; and [2] days since a particular starting time
+or `epoch', usually 1970 in the Unix world and 1980 in the
+MS-DOS/Windows world. If you find that it is not the first meaning
+that you really want, then check out the Date::Manip and Date::Calc
+modules. (Thanks to David Cassell for most of this text.)
=head2 How do I find yesterday's date?
If you are serious about writing a parser, there are a number of
modules or oddities that will make your life a lot easier. There are
the CPAN modules Parse::RecDescent, Parse::Yapp, and Text::Balanced;
-and the byacc program.
+and the byacc program. Starting from perl 5.8 the Text::Balanced
+is part of the standard distribution.
One simple destructive, inside-out approach that you might try is to
pull out the smallest nesting parts one at a time:
This has the strange effect of turning "C<don't do it>" into "C<Don'T
Do It>". Sometimes you might want this. Other times you might need a
-more thorough solution (Suggested by brian d. foy):
+more thorough solution (Suggested by brian d foy):
$string =~ s/ (
(^\w) #at the beginning of the line
If @in is sorted, and you want @out to be sorted:
(this assumes all true values in the array)
- $prev = 'nonesuch';
+ $prev = "not equal to $in[0]";
@out = grep($_ ne $prev && ($prev = $_, 1), @in);
This is nice in that it doesn't use much extra memory, simulating
But perhaps you should have been using a hash all along, eh?
-=head2 How can I tell whether a list or array contains a certain element?
+=head2 How can I tell whether a certain element is contained in a list or array?
Hearing the word "in" is an I<in>dication that you probably should have
used a hash, not a list or array, to store your data. Hashes are
=head2 How do I shuffle an array randomly?
-Use this:
+If you either have Perl 5.8.0 or later installed, or if you have
+Scalar-List-Utils 1.03 or later installed, you can say:
+
+ use List::Util 'shuffle';
+
+ @shuffled = shuffle(@list);
+
+If not, you can use this:
# fisher_yates_shuffle( \@array ) :
# generate a random permutation of @array in place
sub fisher_yates_shuffle {
my $array = shift;
- my $i;
- for ($i = @$array; --$i; ) {
+ my $i = @$array;
+ while (--$i) {
my $j = int rand ($i+1);
@$array[$i,$j] = @$array[$j,$i];
}
fisher_yates_shuffle( \@array ); # permutes @array in place
+Note that the above implementation shuffles an array in place,
+unlike the List::Util::shuffle() which takes a list and returns
+a new shuffled list.
+
You've probably seen shuffling algorithms that work using splice,
randomly picking another element to swap the current element with
}
}
+Unfortunately, this algorithm is very inefficient. The Algorithm::Permute
+module from CPAN runs at least an order of magnitude faster. If you don't
+have a C compiler (or a binary distribution of Algorithm::Permute), then
+you can use List::Permutor which is written in pure Perl, and is still
+several times faster than the algorithm above.
+
=head2 How do I sort an array by (anything)?
Supply a comparison function to sort() (described in L<perlfunc/sort>):
For some specific applications, you can use one of the DBM modules.
See L<AnyDBM_File>. More generically, you should consult the FreezeThaw,
-Storable, or Class::Eroot modules from CPAN. Here's one example using
+Storable, or Class::Eroot modules from CPAN. Starting from Perl 5.8
+Storable is part of the standard distribution. Here's one example using
Storable's C<store> and C<retrieve> functions:
use Storable;
Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington.
All rights reserved.
-When included as part of the Standard Version of Perl, or as part of
-its complete documentation whether printed or otherwise, this work
-may be distributed only under the terms of Perl's Artistic License.
-Any distribution of this file or derivatives thereof I<outside>
-of that package require that special arrangements be made with
-copyright holder.
+This documentation is free; you can redistribute it and/or modify it
+under the same terms as Perl itself.
Irrespective of its distribution, all code examples in this file
are hereby placed into the public domain. You are permitted and