If your text is delimited (commas, tabs, pipe,...) I would use Text::CSV_XS. It will handle about any delimiter you can throw at it.
Code Example:
use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new({ sep_char => '|' }); foreach my $line (<DATA>) { my $status = $csv->parse($line); if (!$status) { print $csv->error_input() . "\n"; next; } my @cols = $csv->fields(); print join(' # ',@cols); print "\n"; } __DATA__ foo|bar|baz aaa|bbb|ccc ddd|eee|fff
If it's some kind of fixed length look at using unpack. unpack may take some learning but it offers great flexibility and IMO much cleaner code.
Code Example:
use strict; use warnings; foreach my $line (<DATA>) { my @cols = unpack("a3a3a3",$line); print join(' # ',@cols); print "\n"; } __DATA__ foobarbaz aaabbbccc dddeeefff
Now that you have an array just take a slice of the columns you want.
my @rtn = @cols[0,2]; #Grab the 1st and 3rd column print join(' # ',@rtn); print "\n";
As other have pointed out, you should help us help you.
UPDATE: Added code examples
| XP matters not. Look at me. Judge me by my XP, do you? |
In reply to Re: Reading selected columns from text file
by grep
in thread Reading selected columns from text file
by beginr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |