in reply to Text::CSV_XS separator character tab

Wild guess: Since single quotes around a string don't cause that string to be interpolated, wouldn't you want "\t" instead of '\t'?
  • Comment on Re: Text::CSV_XS separator character tab

Replies are listed 'Best First'.
Re^2: Text::CSV_XS separator character tab
by Anonymous Monk on May 27, 2005 at 14:59 UTC

    Thanks for the replies, but I tried

    my $csv = Text::CSV_XS->new({ 'quote_char' => '', 'escape_char' => '', 'sep_char' => "\t", 'binary' => 1 });

    But still no joy... the file i'm trying to split up is here: http://www.a4uforum.co.uk/images_a4u/misc/productsYourLenses.txt

      That file has null bytes in it, which seems to interfere with the Text::CSV_XS parser somehow. If you strip the null characters out of the file, it seems to work ok. (It does for me anyway.)

      use warnings; use strict; use Text::CSV_XS; use IO::File; my $file = 'productsYourLenses.txt'; my $fh = new IO::File; $fh->open("< $file") or die "Could not open $file"; my $csv = Text::CSV_XS->new({ 'quote_char' => '', 'escape_char' => '', 'sep_char' => "\t", 'binary' => 1 }); my $columns; while( 1 ){ $columns = $csv->getline($fh); last unless @$columns; print join '|', @$columns, "\n\n"; }

      Or, for something this simple you could just use split.

      use warnings; use strict; my $file = 'productsYourLenses.txt'; open my $fh, '<', $file or die "Could not open $file $!.\n"; my @rows; while(<$fh>){ chomp; s/\x00//g; my @columns = split /\t+/; push @rows, \@columns; } for (@rows){ print join '|', @$_, "\n\n"; }

      Both of these will yield an AoA.

      I was able to parse the file but since I have no idea what data to expect I don't know if it's parsing correctly. What do you mean by "no joy" - that's not very informative. Do you know how the file was produced? Are you sure it actually is a tab-separated file? What are the line endings, they show up as ^@ in emacs.