From 7814084ef7332a6288f2db0374eb7b9b4933fe92 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 4 Dec 2011 18:55:16 +0100 Subject: [PATCH] guess CSV separator char based on first line --- lib/MojoFacets/Import/CSV.pm | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/MojoFacets/Import/CSV.pm b/lib/MojoFacets/Import/CSV.pm index 260c002..c94ab5a 100644 --- a/lib/MojoFacets/Import/CSV.pm +++ b/lib/MojoFacets/Import/CSV.pm @@ -23,10 +23,27 @@ sub data { my $data = { items => [] }; my @header; - my $csv = Text::CSV->new ( { binary => 1, eol => $/ } ) + open my $fh, "<:encoding($encoding)", $path or die "$path: $!"; + my $first = <$fh>; + my $possible_delimiters; + while ( $first =~ s/(\W)// ) { + $possible_delimiters->{$1}++; + } + warn "# possible_delimiters = ",dump($possible_delimiters); + seek $fh,0,0; # rewind for Text::CSV + + my @sep_by_usage = sort { $possible_delimiters->{$b} <=> $possible_delimiters->{$a} } keys %$possible_delimiters; + my $sep_char = shift @sep_by_usage; + while ( $sep_char =~ m/^\s$/ ) { + warn "## skip whitespace separator ",dump($sep_char); + $sep_char = shift @sep_by_usage; + } + + warn "sep_char = [$sep_char] for $path\n"; + + my $csv = Text::CSV->new ( { binary => 1, eol => $/, sep_char => $sep_char } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); - open my $fh, "<:encoding($encoding)", $path or die "$path: $!"; while ( my $row = $csv->getline( $fh ) ) { if ( ! @header ) { @header = @$row; -- 2.20.1