This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at
http://faq.perl.org .
--------------------------------------------------------------------
4.34: How do I extract selected columns from a string?
(contributed by brian d foy)
If you know where the columns that contain the data, you can use
"substr" to extract a single column.
my $column = substr( $line, $start_column, $length );
You can use "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' );