From: Dobrica Pavlinusic Date: Sat, 8 Jul 2023 07:50:14 +0000 (+0200) Subject: correctly quote csv output with spaces in it X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;h=283961c97c87fc0bf3755cce7e4f3a647fd16ccb;p=openface-maja correctly quote csv output with spaces in it --- diff --git a/p.pl b/p.pl index 4eef17a..1886315 100755 --- a/p.pl +++ b/p.pl @@ -72,6 +72,11 @@ sub glob_id { open(my $skipped_input, '>',"out.$today.skip"); open(my $out_fh, '>', "out.$today.csv"); +sub out_csv { + my @a = @_; + print $out_fh join(",", map { m/\s/ ? qq{"$_"} : $_ } @a),"\n"; +} + my @out_header; while(<$fh>) { @@ -155,7 +160,7 @@ while(<$fh>) { push @out_header, map { $h2[$_] } ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} ); warn "# out_header ",dump( \@out_header ); - print $out_fh join(",", @out_header),"\n"; + out_csv @out_header; } next; } @@ -206,7 +211,7 @@ while(<$fh>) { push @add_cols, map { $sum[$_] / $count } ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_r'} ); push @add_cols, map { $sum[$_] } ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} ); warn "# add_cols = ",dump( \@add_cols ) if $debug; - print $out_fh join(",", @v, @add_cols ), "\n"; + out_csv @v, @add_cols; @sum = (); $count = 0;