Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Using TEXT::CSV_XS to delete columns

by PRyanRay (Novice)
on Nov 10, 2012 at 21:47 UTC ( #1003283=perlquestion: print w/ replies, xml ) Need Help??
PRyanRay has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! I have rather ugly csv files I need to compare and am running into trouble with a basic need. I simply want to keep columns I am interested in. One complication is that the columns are not necessarily in the same order in each file. I have the names of the headers however. For instance, how would I output only the columns that use the header names: 'Object ID', 'Object Number', 'Description' I have some code but I would rather forgo the lashing I would recieve by posting it. Here is the skeleton:

#!usr/bin/perl use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/, auto_diag => 0 +}); open my $fh, "<", $ARGV[0] or die "$ARGV[0]: $!\n"; while (1) { my $row = $csv->getline ($fh) or last; }

My first thought was to do an if statement on the first row (where the headers are) and keep track of the column number. This seems cumbersome. So, if "Object ID" is the 3rd column, then use something like this:

while (my $fields = $csv->getline( $fh )) { entry = $fields->[2];}
and output this to a new file. Thank you for the help and I apologize for my ignorance. I looked at the CPAN description and could not figure out how to do this.

Comment on Using TEXT::CSV_XS to delete columns
Select or Download Code
Re: Using TEXT::CSV_XS to delete columns
by CountZero (Bishop) on Nov 10, 2012 at 21:54 UTC
    Look at the CSV-files as a database and use DBD::CSV then you do not need to worry about the sequence of the headers.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Okay, I will give that a look, but is there a way to do it with TEXT::CSV_XS? That is the solution I am looking for. Thanks.
        Of course there is a way to do this.

        Have a look at the column_names and getline_hr (or getline_hr_all) methods of Text::CSV.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003283]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2014-12-19 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (91 votes), past polls