From: Don Armstrong Date: Tue, 27 Nov 2012 02:03:11 +0000 (+0000) Subject: autodetect r mode X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=a0f50a48f47db3337b33310770e0e3d162398708;p=bin.git autodetect r mode --- diff --git a/txt2xls b/txt2xls index 3ede2f4..147ace9 100755 --- a/txt2xls +++ b/txt2xls @@ -61,7 +61,6 @@ use Spreadsheet::WriteExcel; my %options = (debug => 0, help => 0, man => 0, - rmode => 1, remove_name => [], ); @@ -88,8 +87,6 @@ if (1 < grep {exists $options{$_}} qw(tsv ssv csv)) { pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; -my @columns = ('A'..'Z','AA'..'ZZ'); - if (not @ARGV) { # we'll use this as a special indicator to read stdin push @ARGV,undef; @@ -129,14 +126,29 @@ for my $file (@ARGV) { my $row = 1; my @header_row; my $overflow = 0; + my $r_mode = $options{r_mode} // 0; + # set to 1 if we have attempted to autodetect R mode + my $r_mode_autodetected = 0; while (<$fh>) { chomp; # parse the line - my @row; die "Unable to parse line $. of $file" unless $csv->parse($_); - if ($row==1 and $options{rmode}) { - # R doesn't output headers for rownames - push @row,''; + my @row = $csv->fields(); + if ($row==1 and not $r_mode_autodetected) { + @header_row = @row; + $row++; + next; + } + if ($row==2 and not $r_mode_autodetected) { + $r_mode_autodetected = 1; + if (@row == (@header_row+1)) { + $r_mode = 1 unless exists $options{r_mode} and defined $options{r_mode}; + } + if ($r_mode) { + # R doesn't output headers for rownames + unshift @header_row,''; + } + output_row(\@header_row,1,$ws); } if ($row > 65536) { # ok, we're going to overflow here my $t_ws_name = $ws_name; @@ -144,20 +156,23 @@ for my $file (@ARGV) { $t_ws_name =~ s{^(.{0,$maxlen}).*$}{$1}; $ws = $wb->add_worksheet($ws_name.'.'.$overflow); $row=1; - for my $i (0..@header_row) { - $ws->write($columns[$i].$row,$header_row[$i]); - } + output_row(\@header_row,$row,$ws); $row++; } - push @row,$csv->fields(); if ($row==1) { @header_row = @row; } - for my $i (0..$#row) { - $ws->write($columns[$i].$row,$row[$i]); - } + output_row(\@row,$row,$ws); $row++; } } +sub output_row{ + my ($data,$row,$ws) = @_; + my @columns = ('A'..'Z','AA'..'ZZ'); + for my $i (0..$#{$data}) { + $ws->write($columns[$i].$row,$data->[$i]); + } +} + __END__