[svn:perlfaq] r7863 - perlfaq/trunk
[email protected] Sat, 30 Sep 2006 13:31:23 -0700 (PDT)
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
Author: comdog
Date: Sat Sep 30 13:31:23 2006
New Revision: 7863
Modified:
perlfaq/trunk/perlfaq3.pod
perlfaq/trunk/perlfaq4.pod
Log:
* perlfaq3: Why don't Perl one-liners work on my DOS/Mac/VMS system?
+ denote the difference between Mac OS X and Mac Classic
+ Thanks to Sherm Pendley
* perlfaq4: How do I extract selected columns from a string?
+ Rewrite answer (which only half answered it before)
+ Added examples with split, but mention the CSV modules
+ Show an unpack example
+ Thanks to Jim Gibson
Modified: perlfaq/trunk/perlfaq3.pod
==============================================================================
--- perlfaq/trunk/perlfaq3.pod (original)
+++ perlfaq/trunk/perlfaq3.pod Sat Sep 30 13:31:23 2006
@@ -877,13 +877,13 @@
For example:
- # Unix
+ # Unix (including Mac OS X)
perl -e 'print "Hello world\n"'
# DOS, etc.
perl -e "print \"Hello world\n\""
- # Mac
+ # Mac Classic
print "Hello world\n"
(then Run "Myscript" or Shift-Command-R)
Modified: perlfaq/trunk/perlfaq4.pod
==============================================================================
--- perlfaq/trunk/perlfaq4.pod (original)
+++ perlfaq/trunk/perlfaq4.pod Sat Sep 30 13:31:23 2006
@@ -958,25 +958,39 @@
=head2 How do I extract selected columns from a string?
-Use C<substr()> or C<unpack()>, both documented in L<perlfunc>.
-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<substr> to extract a single column.
+
+ my $column = substr( $line, $start_column, $length );
+
+You can use C<split> 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<Text::CSV>, C<Text::CSV_XS>, or
+C<Text::CSV_PP>.
+
+If you want to break apart an entire line of fixed columns, you can use
+C<unpack> with the A (ASCII) format. by using a number after the format
+specifier, you can denote the column width. See the C<pack> and C<unpack>
+entries in L<perlfunc> for more details.
+
+ my @fields = unpack( $line, "A8 A8 A8 A16 A4" );
+
+Note that spaces in the format argument to C<unpack> do not denote literal
+spaces. If you have space separated data, you may want C<split> instead.
=head2 How do I find the soundex value of a string?
@@ -1014,11 +1028,10 @@
=head2 What's wrong with always quoting "$vars"?
The problem is that those double-quotes force
-stringification--coercing numbers and references into
-strings--even when you don't want them to be strings. Think
-of it this way: double-quote expansion is used to produce
-new strings. If you already have a string, why do you need
-more?
+stringification--coercing numbers and references into strings--even
+when you don't want them to be strings. Think of it this way:
+double-quote expansion is used to produce new strings. If you already
+have a string, why do you need more?
If you get used to writing odd things like these: