X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq4.pod;h=5e10a11ad564b329504ad2b6dce851bfe1abb824;hb=8dab6df03348b64bdb09672e6ba507c8969ec2c7;hp=f7215e2eef99a8e7ea8b1ff231776b37f4add881;hpb=197aec242db45fbf1d7853a1ae22a108cc09d23c;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index f7215e2..5e10a11 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq4 - Data Manipulation ($Revision: 1.39 $, $Date: 2003/01/03 20:06:21 $) +perlfaq4 - Data Manipulation ($Revision: 1.44 $, $Date: 2003/07/28 17:35:21 $) =head1 DESCRIPTION @@ -142,7 +142,7 @@ Using the CPAN module Bit::Vector: =item How do I convert from decimal to hexadecimal -Using sprint: +Using sprintf: $hex = sprintf("%X", 3735928559); @@ -316,7 +316,7 @@ Get the http://www.cpan.org/modules/by-module/Roman module. If you're using a version of Perl before 5.004, you must call C once at the start of your program to seed the random number generator. - BEGIN { srand() if $[ < 5.004 } + BEGIN { srand() if $] < 5.004 } 5.004 and later automatically call C at the beginning. Don't call C more than once---you make your numbers less random, rather @@ -353,13 +353,34 @@ C =head1 Data: Dates -=head2 How do I find the week-of-the-year/day-of-the-year? +=head2 How do I find the day or week of the year? + +The localtime function returns the day of the week. Without an +argument localtime uses the current time. + + $day_of_year = (localtime)[7]; + +The POSIX module can also format a date as the day of the year or +week of the year. -The day of the year is in the array returned by localtime() (see -L): + use POSIX qw/strftime/; + my $day_of_year = strftime "%j", localtime; + my $week_of_year = strftime "%W", localtime; + +To get the day of year for any date, use the Time::Local module to get +a time in epoch seconds for the argument to localtime. + + use POSIX qw/strftime/; + use Time::Local; + my $week_of_year = strftime "%W", + localtime( timelocal( 0, 0, 0, 18, 11, 1987 ) ); - $day_of_year = (localtime(time()))[7]; +The Date::Calc module provides two functions for to calculate these. + use Date::Calc; + my $day_of_year = Day_of_Year( 1987, 12, 18 ); + my $week_of_year = Week_of_Year( 1987, 12, 18 ); + =head2 How do I find the current century or millennium? Use the following simple functions: @@ -741,6 +762,19 @@ case", but that's not quite accurate. Consider the proper capitalization of the movie I, for example. +Damian Conway's L module provides some smart +case transformations: + + use Text::Autoformat; + my $x = "Dr. Strangelove or: How I Learned to Stop ". + "Worrying and Love the Bomb"; + + print $x, "\n"; + for my $style (qw( sentence title highlight )) + { + print autoformat($x, { case => $style }), "\n"; + } + =head2 How can I split a [character] delimited string except when inside [character]? Several modules can handle this sort of pasing---Text::Balanced, @@ -811,9 +845,6 @@ values of a hash if you use a slice: =head2 How do I pad a string with blanks or pad a number with zeroes? -(This answer contributed by Uri Guttman, with kibitzing from -Bart Lateur.) - In the following examples, C<$pad_len> is the length to which you wish to pad the string, C<$text> or C<$num> contains the string to be padded, and C<$pad_char> contains the padding character. You can use a single @@ -828,13 +859,16 @@ right with blanks and it will truncate the result to a maximum length of C<$pad_len>. # Left padding a string with blanks (no truncation): - $padded = sprintf("%${pad_len}s", $text); + $padded = sprintf("%${pad_len}s", $text); + $padded = sprintf("%*s", $pad_len, $text); # same thing # Right padding a string with blanks (no truncation): - $padded = sprintf("%-${pad_len}s", $text); + $padded = sprintf("%-${pad_len}s", $text); + $padded = sprintf("%-*s", $pad_len, $text); # same thing # Left padding a number with 0 (no truncation): - $padded = sprintf("%0${pad_len}d", $num); + $padded = sprintf("%0${pad_len}d", $num); + $padded = sprintf("%0*d", $pad_len, $num); # same thing # Right padding a string with blanks using pack (will truncate): $padded = pack("A$pad_len",$text); @@ -958,13 +992,13 @@ Stringification also destroys arrays. print "@lines"; # WRONG - extra blanks print @lines; # right -=head2 Why don't my <EHERE documents work? Check for these three things: =over 4 -=item There must be no space after the << part. +=item There must be no space after the EE part. =item There (probably) should be a semicolon at the end. @@ -1870,7 +1904,7 @@ it on top of either DB_File or GDBM_File. Use the Tie::IxHash from CPAN. use Tie::IxHash; - tie my %myhash, Tie::IxHash; + tie my %myhash, 'Tie::IxHash'; for (my $i=0; $i<20; $i++) { $myhash{$i} = 2*$i; }