X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq4.pod;h=3200e7aca48e6c021172385b869788702eecee15;hb=0c42fe95656e99f238a0bcf90ab2476c175615b7;hp=e9c4ab316a618990aafa0d7da03b16f8640b5669;hpb=ac9dac7f0e1dffa602850506b980a255334a4f40;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index e9c4ab3..3200e7a 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq4 - Data Manipulation ($Revision: 6816 $) +perlfaq4 - Data Manipulation ($Revision: 10394 $) =head1 DESCRIPTION @@ -17,7 +17,7 @@ exactly. Some real numbers lose precision in the process. This is a problem with how computers store numbers and affects all computer languages, not just Perl. -L show the gory details of number representations and +L shows the gory details of number representations and conversions. To limit the number of decimal places in your numbers, you can use the @@ -362,17 +362,16 @@ pseudorandom generator than comes with your operating system, look at =head2 How do I get a random number between X and Y? -To get a random number between two values, you can use the -C builtin to get a random number between 0 and +To get a random number between two values, you can use the C +builtin to get a random number between 0 and 1. From there, you shift +that into the range that you want. -C returns a number such that -C<< 0 <= rand($x) < $x >>. Thus what you want to have perl -figure out is a random number in the range from 0 to the -difference between your I and I. +C returns a number such that C<< 0 <= rand($x) < $x >>. Thus +what you want to have perl figure out is a random number in the range +from 0 to the difference between your I and I. -That is, to get a number between 10 and 15, inclusive, you -want a random number between 0 and 5 that you can then add -to 10. +That is, to get a number between 10 and 15, inclusive, you want a +random number between 0 and 5 that you can then add to 10. my $number = 10 + int rand( 15-10+1 ); @@ -491,14 +490,14 @@ give you the same time of day, only the day before. print "Yesterday was $yesterday\n"; -You can also use the C module using its Today_and_Now +You can also use the C module using its C function. use Date::Calc qw( Today_and_Now Add_Delta_DHMS ); my @date_time = Add_Delta_DHMS( Today_and_Now(), -1, 0, 0, 0 ); - print "@date\n"; + print "@date_time\n"; Most people try to use the time rather than the calendar to figure out dates, but that assumes that days are twenty-four hours each. For @@ -600,7 +599,9 @@ anonymous array. In this case, we call the function in list context. If we want to call the function in scalar context, we have to do a bit more work. We can really have any code we like inside the braces, so we simply have to end with the scalar reference, although how you do -that is up to you, and you can use code inside the braces. +that is up to you, and you can use code inside the braces. Note that +the use of parens creates a list context, so we need C to +force the scalar context on the function: print "The time is ${\(scalar localtime)}.\n" @@ -958,25 +959,39 @@ Left and right padding with any character, modifying C<$text> directly: =head2 How do I extract selected columns from a string? -Use C or C, both documented in L. -If you prefer thinking in terms of columns instead of widths, -you can use this kind of thing: - - # determine the unpack format needed to split Linux ps output - # arguments are cut columns - my $fmt = cut2fmt(8, 14, 20, 26, 30, 34, 41, 47, 59, 63, 67, 72); - - sub cut2fmt { - my(@positions) = @_; - my $template = ''; - my $lastpos = 1; - for my $place (@positions) { - $template .= "A" . ($place - $lastpos) . " "; - $lastpos = $place; - } - $template .= "A*"; - return $template; - } +(contributed by brian d foy) + +If you know where the columns that contain the data, you can +use C to extract a single column. + + my $column = substr( $line, $start_column, $length ); + +You can use C if the columns are separated by whitespace or +some other delimiter, as long as whitespace or the delimiter cannot +appear as part of the data. + + my $line = ' fred barney betty '; + my @columns = split /\s+/, $line; + # ( '', 'fred', 'barney', 'betty' ); + + my $line = 'fred||barney||betty'; + my @columns = split /\|/, $line; + # ( 'fred', '', 'barney', '', 'betty' ); + +If you want to work with comma-separated values, don't do this since +that format is a bit more complicated. Use one of the modules that +handle that fornat, such as C, C, or +C. + +If you want to break apart an entire line of fixed columns, you can use +C with the A (ASCII) format. by using a number after the format +specifier, you can denote the column width. See the C and C +entries in L for more details. + + my @fields = unpack( $line, "A8 A8 A8 A16 A4" ); + +Note that spaces in the format argument to C do not denote literal +spaces. If you have space separated data, you may want C instead. =head2 How do I find the soundex value of a string? @@ -988,37 +1003,64 @@ C, and C modules. =head2 How can I expand variables in text strings? -Let's assume that you have a string that contains placeholder -variables. - - $text = 'this has a $foo in it and a $bar'; - -You can use a substitution with a double evaluation. The -first /e turns C<$1> into C<$foo>, and the second /e turns -C<$foo> into its value. You may want to wrap this in an -C: if you try to get the value of an undeclared variable -while running under C, you get a fatal error. +(contributed by brian d foy) - eval { $text =~ s/(\$\w+)/$1/eeg }; - die if $@; +If you can avoid it, don't, or if you can use a templating system, +such as C or C