Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to parse a tab delimited file

by existem (Sexton)
on Nov 25, 2004 at 17:36 UTC ( #410430=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I am using Text::CSV to parse csv files and it works beautifully, however i've just been passed a file which uses tabs as the delimiter, and I thought there must be a switch in the module somewhere to ask it to use tab delimiter rather than comma, but I can't see one... any suggestions?

Thanks, Tom

2004-11-26 Edited by Arunbear: Changed title from 'CSV::Text', as per Monastery guidelines

Replies are listed 'Best First'.
Re: How to parse a tab delimited file
by TStanley (Canon) on Nov 25, 2004 at 17:40 UTC
    In this case, you might want to switch to Text::xSV, which will handle it. This module also happens to have been written by one of our members.

    Update: Text::CSV_XS will also do what you need.

    TStanley
    --------
    The only thing necessary for the triumph of evil is for good men to do nothing -- Edmund Burke

      Thanks for the help

      Sorry for being a completely stupid monk, but how do I use this?

      How can I call the new constructor setting the separator to tab (\t)?

      I have this so far but it doesn't work.

      my $csv = Text::xSV->new( filename => $csv_destination, sep => '\t', ); $csv->open_file("foo.csv"); $csv->read_header(); foreach my $field ($csv->get_fields) { if (lc($field) ne $field) { $csv->alias($field, lc($field)); } }

      Complains about using The separator '\t' is not of length 1.

      Thanks,
      Tom

        sep => '\t',

        The separator '\t' is not of length 1.

        You get rid of that error if you specify it in double quotes:

        sep => "\t",

        Cheers, Sören

Re: How to parse a tab delimited file
by diotalevi (Canon) on Nov 25, 2004 at 17:47 UTC

    If you use Text::CSV_XS you've got that feature and more.

    my $csv = Text::CSV_XS->new( sep_char => "\t", escape_char => "\\", quote_char => undef );
Re: How to parse a tab delimited file
by saberworks (Curate) on Nov 25, 2004 at 20:38 UTC
    I have used Text::CSV_XS to do this, but I found Text::Delimited to be easier to use because it parses the data into nice hashrefs similar to what comes out of DBI's fetchrow_hashref().
Re: How to parse a tab delimited file
by radiantmatrix (Parson) on Nov 26, 2004 at 14:55 UTC

    All of the modules I've seen mentioned so far would work, but as I've just solved this same problem (multiple delimiters for different files that must go through the same code), I will offer this: Text::xSV benchmarked much faster than anything other than Text::CSV_XS -- and it was close between them. The latter, however, isn't available through ASPN, so not so good for ActivePerl/Windows users.


    radiantmatrix
    require General::Disclaimer;
    Perl is

      The latter, however, isn't available through ASPN, so not so good for ActivePerl/Windows users.

      Thats strange, I was able to use PPM to download and install both Text::xSV and Text::CSV_XS. I'm using ActiveState Perl 5.8 (Build 806) on Windows XP Home Edition

      TStanley
      --------
      The only thing necessary for the triumph of evil is for good men to do nothing -- Edmund Burke

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-08-14 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?