+ elsif ($cur_row != 0) {
+ print {$out} $sep_char;
+ }
+ # write it to the output file
+ print {$out} $next_field;
+ if ($in->eof) {
+ # avoid writing out a newline if the file was totally empty to start with
+ print {$out} "\n" if not $first_time;
+ last;
+ }
+
+ # if this is the first time through, store this position for
+ # this row, then find the end of the row [field with a \n as a
+ # terminator] and do the next loop; if we hit the end of the
+ # file, we are no longer the first time through.
+ $row[$cur_row] = $in->tell;
+ if ($first_time) {
+ print STDERR "\r$cur_row";
+ $cur_row++;
+ advance_to_field($in,"\n");
+ $first_row_end = $in->tell if not defined $first_row_end;
+ if ($in->eof()) {
+ $first_time = 0;
+ $in->seek($row[0],SEEK_SET);
+ $cur_row = 0;
+ }
+ }
+ else {
+ # otherwise, advance to the next row's position
+ $cur_row = ($cur_row + 1) % @row;
+ $in->seek($row[$cur_row],SEEK_SET);
+ }
+ if ($cur_row == 0) {
+ print STDERR "\r".$in->tell."/$first_row_end";
+ }
+ } while (1);