in reply to Re: Question on the format statement
in thread Question on the format statement

Thanks, I read the second para.

Thank you everyone else for your advice.

Here is the code which I'm trying and it doesnt work. I modified some code i found online.
open DR, ">ddailyreport.prn" or die "Cant open file"; format DATA = @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<< @##### @############### + Rs@#####.## ~ $consname, $subdvn, $chequeno, $receiptno, $amount . format TOTAL = -------------------------------------------------------------------- +----------------------- + Rs@#####.## + $total . format TOP = @|||||||||||||||||||||||||||||||||||| Pg @< "Daily Deposit Works Report", $% Consumer name Subdivision Cheque no. Receipt no. + Amount -------------- -------------- ------------- ------------- +- ------- . sub dotize { my($width, $string) = @_; if (length($string) > $width) { return(substr($string, 0, $width - 3) . "..."); } else { return($string); } } $~ = "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); $~ = "DATA"; write(DR); $total += $amount; } $~ = "TOTAL"; write(DR); close(DR);
What is wrong in this piece of code?

Replies are listed 'Best First'.
Re^3: Question on the format statement
by roboticus (Chancellor) on Apr 01, 2010 at 14:33 UTC

    perl_seeker:

    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

      Thank you :)

      Probably should have read some more before posting. Well the code does not work if some of the fields are left undefined i.e. this section of code:
      $consname = "" if !defined($consname); $subdvn = "" if !defined($subdvn); $chequeno = 0 if !defined($chequeno); $receiptno = 0 if !defined($receiptno); $amount= 0 if !defined($amount);
      Sample data:
      M/s. Something Pvt. Ltd.!Elm Street!134!27456.00 ---no cheque no. Somethin&Someone!New Park Street!29787!135!28477.00 Something-Something!Washington!136!28476.00 ---no cheque no. Someone!Pole Street!38947!137!28376.00
      Also for variable length fields e.g, $consname,$subdvn, the space between the columns in the report is not adjusted.
      If the report contained several rows of data formatting the space between columns would be tedious. Is there a solution to this?

        perl seeker:

        I don't quite follow--I just added your sample data to the program, ran it, and don't see a problem. When you say "the code does not work if some of the fields are left undefined", what do you mean exactly? I'm not seeing any problems.

        Regarding your comment on the space between columns not being adjusted, I don't know what you mean. Do you mean that the column headers don't match the numbers? If so, just adjust your format statements a bit. Or do you want all the columns to spread apart if one of the columns is wider than expected and shrink when a column is narrower than expected? If so, that's a bit more complicated...

        ...roboticus