beginr has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Reading selected columns from text file

Replies are listed 'Best First'.
Re: Reading selected columns from text file
by davorg (Chancellor) on Nov 29, 2006 at 15:25 UTC

    Yes. Write your program so that it ignores the columns that you don't want to see.

    I can give more details - but you need to go first :-) Please read How (Not) To Ask A Question.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Reading selected columns from text file
by liverpole (Monsignor) on Nov 29, 2006 at 15:42 UTC
    Hi beginr,

    What's your definition of "column"?

    Is it always a fixed number of characters, starting at a fixed character position in the file?  That will be quite easy in Perl.

    Or does it depend on how you "delimit" your columns?  For example, if your data is always separated by whitespace (eg. spaces or tabs), or some other well-defined set of characters, then that's quite easy too.

    The problem is, we can't solve your problem until we know what you need to do.  Even then, we need to know you've tried for yourself, and where you're getting stuck.  It wouldn't do us much good to say "After you've read the file into memory, do the following", and then find out that you're stuck on how to open and read a file from disk.  Nor would it productive to say "Just use regexes to extract the data you want" if it's regular expressions that you're unclear about.

    And most importantly, if you have any code that you've written, by all means show us that, as an example program is almost always the best starting point.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Reading selected columns from text file
by grep (Monsignor) on Nov 29, 2006 at 15:59 UTC

    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

    grep
    XP matters not. Look at me. Judge me by my XP, do you?

Re: Reading selected columns from text file
by GrandFather (Saint) on Nov 29, 2006 at 20:07 UTC