D
Domenico Discepola
Hello. I'm trying to parse a text file into a 2-d array using Text::CSV_XS.
The input file is structured as follows. "Fields" are separated with a
"\x0d\x0a" (CRLF) and are enclosed in double-quotes. "Records" are
separated with a "\x0c" (FF). My fields can contain embedded CRLF's hence
the need for double-quoting. How can I use Text::CSV_XS to solve my
problem? My code below only outputs the first line in the input file.
Thanks in advance.
#!perl
use strict;
use warnings;
use diagnostics;
use Text::CSV_XS;
our $g_file_input = shift @ARGV;
die "Usage: $0 filename\n" unless $g_file_input;
######
my ( @arr01 );
#Record seperator - I tried using this and commenting this out
# local $/ = "\x0c";
my $csv = Text::CSV_XS->new( {'sep_char' => "\x0d\x0a", 'binary' => 1,
'always_quote' => 1 } );
open(TFILE, "< ${g_file_input}") || die "$!";
while (<TFILE>) {
my $line = $_;
my $status = $csv->parse($line) || print "Cannot parse\n";
my @arr_temp = $csv->fields();
push ( @arr01, [@arr_temp]);
print join('|', $_), "\n" for @arr_temp;
#exiting here for debugging only
exit;
}
close (TFILE) || die "$!\n";
The input file is structured as follows. "Fields" are separated with a
"\x0d\x0a" (CRLF) and are enclosed in double-quotes. "Records" are
separated with a "\x0c" (FF). My fields can contain embedded CRLF's hence
the need for double-quoting. How can I use Text::CSV_XS to solve my
problem? My code below only outputs the first line in the input file.
Thanks in advance.
#!perl
use strict;
use warnings;
use diagnostics;
use Text::CSV_XS;
our $g_file_input = shift @ARGV;
die "Usage: $0 filename\n" unless $g_file_input;
######
my ( @arr01 );
#Record seperator - I tried using this and commenting this out
# local $/ = "\x0c";
my $csv = Text::CSV_XS->new( {'sep_char' => "\x0d\x0a", 'binary' => 1,
'always_quote' => 1 } );
open(TFILE, "< ${g_file_input}") || die "$!";
while (<TFILE>) {
my $line = $_;
my $status = $csv->parse($line) || print "Cannot parse\n";
my @arr_temp = $csv->fields();
push ( @arr01, [@arr_temp]);
print join('|', $_), "\n" for @arr_temp;
#exiting here for debugging only
exit;
}
close (TFILE) || die "$!\n";