Jeffro Tull has asked for the wisdom of the Perl Monks concerning the following question:

I have some code that writes some data to a file. The problem is that I want the data to be written onto one line with the pipe separator. Unfortunately, the data is being written to a newline each time. I've used chomp to fix the line outside of the foreach, but no matter how I try chomp within the foreach, I can't seem to get it to work. I have included the code and the output file.

sub write_results { if($regtype eq "biz") { open (RESULTSDB, ">>$datadir/databases/bizresults\.dat") || di +e "Cannot open bizresults\n"; $writebiz = "$complogin|$first|$last|$email|$test_taken|$test_ +score"; chomp ($writebiz); print RESULTSDB $writebiz; } elsif ($regtype eq "ind") { open (RESULTSDB, ">>$datadir/databases/indresults\.dat") || di +e "Cannot open indresults\n"; $writeind = "$username|$test_taken|$test_score"; chomp ($writeind); print RESULTSDB $writeind; }#end If foreach $sorted_row2 (@sorted_rows) { ($number_missed2, $report_category_name2) = split (/\|/,$sorte +d_row2); chomp($number_missed2, $report_category_name2); $foreach = "|$report_category_name2|$number_missed2"; chomp ($foreach); print RESULTSDB $foreach; }#end foreach print RESULTSDB "\n"; close (RESULTSDB); }


Output:
company|first|last|email@email.com|test taken|score|Edit Commands |7|Settings |6|Dimensioning |4|Draw Commands |4|Coordinate Systems |3|Grip editing |2|Text Commands |2|File Commands |2|Layers |2|Selection Sets |2|Hatching |1|Utilties |1|Plotting |1|Inquiry Commands |1|Display |1|Block Commands |1

Replies are listed 'Best First'.
Re: File Writing and newline
by strat (Canon) on Feb 06, 2002 at 14:22 UTC
    Maybe you could try the following instead of chomp:
    $foreach =~ s/\r?\n$//g;

    Best regards,
    perl -e "$_=*F=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: File Writing and newline
by talexb (Chancellor) on Feb 06, 2002 at 14:22 UTC
    If you haven't adjusted the value of $/, then I guess it's time for the debugger.

    --t. alex

    "Of course, you realize that this means war." -- Bugs Bunny.

Re: File Writing and newline
by redsquirrel (Hermit) on Feb 06, 2002 at 15:15 UTC
    My first advice would be to use strict and -w.

    Then I would advise you to tell/show us how you are getting this data.

    I'm also a bit confused, do you want your data to be written out like this?

    company|first|last|email@email.com|test taken|score|Edit Commands |7|Settings|6|Dimensioning|4|Draw Commands|4|Coordinate Systems|3|Grip + editing|2|Text Commands|2|File Commands|2|Layers|2|Selection Sets|2| +Hatching|1|Utilties|1|Plotting|1|Inquiry Commands|1|Display|1|Block C +ommands|1

    --Dave

Re: File Writing and newline
by jlongino (Parson) on Feb 06, 2002 at 16:43 UTC
    My guess is that you have some combination of multiple carriage return and/or newline at (or before) the end of one or more of your variables (although I suspect $report_category_name2). Instead of chomping anything, try:
    $string_to_print =~ s/\r|\n//g;
    and then print the string. This worked in my tests but then I stink at regexen, so, if this is not the best (correct) way to do it I'd like to know for future reference. BTW, if I were you I'd also pinpoint the offending data source, especially if it is used by other programs.

    --Jim

Re: File Writing and newline
by Speedy (Monk) on Feb 06, 2002 at 18:00 UTC
    "perldoc -f print"

    indicates that the output record separator $\ is printed at the end of each list. I suspect that the output record separator "$\" has somehow gotten set to "\n" rather than a null value.

    At the start of the subroutine, try:

    $\ = "";

    to reset the output record separator and see if it does the trick.
      local $\=''; would be much safer, as it make sure the variable returns to it's previous state when it goes out of scope.

      ~Particle