in reply to Re^2: Question on the format statement
in thread Question on the format statement
How doesn't it work? Specifically: What did you expect to see, what *did* you see, and what were the error messages (if any) that you received?
Looking over your code, I'd change a few things, anyway:
open DR, ">ddailyreport.prn" or die "Can't open file"; # Naming a format the same as a file handle automatically associates t +he format # to the data handle. format DR = @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<< @##### @############### + Rs@#####.## ~ $consname, $subdvn, $chequeno, $receiptno, $amount . # Similarly, adding _TOP to the format name gives you automatic top-of +-form # handling. format DR_TOP = @|||||||||||||||||||||||||||||||||||| Pg @< "Daily Deposit Works Report", $% Consumer name Subdivision Cheque no. Receipt no. + Amount -------------- -------------- ------------- ------------- +- ------- . format TOTAL = -------------------------------------------------------------------- +----------------------- + Rs@#####.## + $total . sub dotize { my($width, $string) = @_; if (length($string) > $width) { return(substr($string, 0, $width - 3) . "..."); } else { return($string); } } # Two things: # (1) This is not needed, per notes above... # (2) The $~ variable associates a format FOR THE DEFAULT OUTPUT # STREAM, so you just changed it for stdout. You would really # use: # my $ofh = select(DR); # make DR the default output # $~ = "DR"; # set format for detail lines # $^ = "DR_TOP"; # set format for top-of-page # select($ofh); # Restore default output stream # #$~ = "TOP"; #write(DR); open(FILE, "<ddailyreport.txt"); @lines = <FILE>; close(FILE); $total = 0; foreach (@lines) { chop(); ($consname, $subdvn, $chequeno, $receiptno, $amount) = (split(/!/) +); $consname = "" if !defined($consname); $subdvn = "" if !defined($subdvn); $chequeno = 0 if !defined($chequeno); $receiptno = 0 if !defined($receiptno); $amount= 0 if !defined($amount); # You need only do the association of the format once. You only n +eed # to do it again if you're going to change it... #$~ = "DATA"; write(DR); $total += $amount; } # Now we change the output format for your summary my $ofh = select(DR); $~ = "TOTAL"; select($ofh); write(DR); close(DR);
...roboticus
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Question on the format statement
by perl_seeker (Scribe) on Apr 06, 2010 at 12:16 UTC | |
by roboticus (Chancellor) on Apr 06, 2010 at 13:48 UTC | |
by perl_seeker (Scribe) on Apr 07, 2010 at 09:38 UTC | |
by roboticus (Chancellor) on Apr 07, 2010 at 18:12 UTC |