Link to home
Start Free TrialLog in
Avatar of steve2312
steve2312Flag for United States of America

asked on

Why does my outfile put out a trailing "," at end of each line ?

The below code snippet  generates a extra "," at end of each line.
The first line prints out without the comma at end of line. The extra "," occurs from second line onward..

How to prevent the trailing , ?

      
my @allrows;
      my @columns;
      my $getfilename = join '', $partition_key,'.csv';
      my $rc  = $genextract->fetchrow_hashref() or $nodata = 1;
               my @row = @{$genextract ->{NAME}};

      foreach my $col (@row) {
          my $name = $col;
          $name =~ s/\s+//g;
          my $data = $rc->{$name};
          push @allrows, $data;
          push @columns, $name;
      }
      my $dumpdir = "$wkdir/RECgen_$getfilename";

      open (OUT, ">$dumpdir") or die(" - Could not open $dumpdir for export. $!");

      # List out all the columns/rows with header information and generating a csv outfile..
      $" = ",";
      print OUT "@columns\n";
      print OUT "@allrows\n";

      while (my $row = $genextract->fetchrow_hashref()) {
      
        foreach my $col (@columns) {
     
           print OUT $$row{$col},",";        
        }
        print OUT "\n";
      }
      close(OUT);

Open in new window



output given
10/01/2015,0784626443000,ACACIA,ACACIA_UNIT_1,BRYAN SOLAR LLC (RE),RN,80.743461
10/01/2015,0432268853000,AUSTPL,BOULDERPL_DENV2,LOWER COLORADO RIVER AUTHORITY (RE),OS,7.656099,
10/01/2015,930187393,AV,AV_DG1,TX LFG ENERGY LP (RE),RN,94.34616,
10/01/2015,0791659493000,BAFFIN,BAFFIN_UNIT1,BAFFIN WIND LLC (RE),RN,101.595611,
10/01/2015,0791659493000,BAFFIN,BAFFIN_UNIT2,BAFFIN WIND LLC (RE),RN,110.031439,

Open in new window



EXPECTED OUTPUT
10/01/2015,0784626443000,ACACIA,ACACIA_UNIT_1,BRYAN SOLAR LLC (RE),RN,80.743461
10/01/2015,0432268853000,AUSTPL,BOULDERPL_DENV2,LOWER COLORADO RIVER AUTHORITY (RE),OS,7.656099
10/01/2015,930187393,AV,AV_DG1,TX LFG ENERGY LP (RE),RN,94.34616
10/01/2015,0791659493000,BAFFIN,BAFFIN_UNIT1,BAFFIN WIND LLC (RE),RN,101.595611
10/01/2015,0791659493000,BAFFIN,BAFFIN_UNIT2,BAFFIN WIND LLC (RE),RN,110.031439

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of steve2312

ASKER

Thanks for that suggestion. That worked