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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.