2012-02-08 8 views
1

printfを使用してこの出力例を印刷するにはどうすればよいですか?Perl printf issue

****************************************************************************** 
** XYZ Corporation     Date: March 27, 1989(Use current date)** 
** 999 John Doe street              ** 
** Ypsilanti, MI. 48197.             ** 
**                   ** 
** Pay to the order of: ?             ** 
**    The amount of: ?  dollars, and ? cents    ** 
**                   ** 
**         signed:         ** 
**           President, XYZ Corporation.  ** 
**                   ** 
**--------------------------------------------------------------------------** 
**       SUMMARY           ** 
** Social security number: ?            ** 
** Regular pay:    ?            ** 
** Overtime pay:    ?            ** 
** Gross pay:    ?            ** 
** Federal tax:    ?            ** 
** Social sec. deduction: ?            ** 
** City tax:     ?            ** 
** Union dues:    ?            ** 
** Net pay:     ?            ** 
**                   ** 
****************************************************************************** 

私は私の方法を試してみましたが、私は右だかどうかわからないです:

printf" 
XYZ Corporation     Date: 
999 John Doe street              
Ypsilanti, MI. 48197             

    Pay to the order of: |             
    The amount of: |  dollars, and | cents    

           signed:         
             President, XYZ Corporation.  

-------------------------------------------------------------------------- 
          SUMMARY           
    Social security number: $ssn           
    Regular pay:    %-.2f 
    Overtime pay:    %-.2f 
    Gross pay:    %-.2f 
    Federal tax:    %-.2f 
    Social sec. deduction: %-.2f 
    City tax:     %-.2f 
    Union dues:    %-.2f 
    Net pay:     %-.2f\n", $regPay, $overPay, $grossPay, $fedTax, $ssnDeduction, $cityTax, $unionDues, $netPay; 

誰も私を助けることができますか?私は私の割り当てが間違っていたと確信していますが、私は解決策を知りたいだけです。

答えて

6

ここでprintfを使用する必要はありません。これは、Perlのformat機能のための完全なアプリケーションのようです。これらは、創業以来言語にあったものであり、なぜPerlは「実用的な抽出と報告言語」の頭字語であるのでしょうか。私はフォーマットを使用したことがないが、あなたは、このチュートリアルでより多くを学ぶことができます。私の知る限りhttp://www.webreference.com/programming/perl/format/index.html

、これは過去20年間非常に少ないを変更した機能なので、ちょうどあなたが見つけるものについてあなたの役に立つ助けを与えるべきである。

+1

://search.cpan:http://perldoc.perl.org/perlform.html – flesk

+2

私は[Perl6の::フォーム](HTTPを聞きました。 org/perldoc?Perl6 :: Form)(Perl5のPerl6形式のフォーム)は、Perl5の組み込み形式よりも優れています。 YMMV。 – ikegami

1
  • ほとんどのフィールドには値を指定していません。
  • あなたは星の箱を取り外しました。
  • 値をパターンに補間しました。
  • フォームの上部に空白行を追加しました。

ソリューション:完全なリファレンスについては

use POSIX qw(strftime); 

my $date = strftime("%B %d, %Y", localtime); 

# Doing it this way prevents floating point rounding errors. 
my $net_pay_x100 = sprintf("%.0f", $net_pay * 100); 
my $net_pay_cents = $net_pay_x100 % 100; 
my $net_pay_dollars = ($net_pay_x100 - $net_pay_cents)/100; 

printf(<<'__EOI__', 
****************************************************************************** 
** XYZ Corporation     Date: %-31s ** 
** 999 John Doe street              ** 
** Ypsilanti, MI. 48197.             ** 
**                   ** 
** Pay to the order of: %-50s ** 
**    The amount of: %5d dollars, and %02d cents     ** 
**                   ** 
**         signed:         ** 
**           President, XYZ Corporation.  ** 
**                   ** 
**--------------------------------------------------------------------------** 
**       SUMMARY           ** 
** Social security number: %11s          ** 
** Regular pay:   %7.2f           ** 
** Overtime pay:   %7.2f           ** 
** Gross pay:    %7.2f           ** 
** Federal tax:   %7.2f           ** 
** Social sec. deduction: %7.2f           ** 
** City tax:    %7.2f           ** 
** Union dues:    %7.2f           ** 
** Net pay:    %7.2f           ** 
**                   ** 
****************************************************************************** 
__EOI__ 
    $date, 
    $name, 
    $net_pay_dollars, 
    $net_pay_cents, 
    $ssn, 
    $reg_pay, 
    $over_pay, 
    $gross_pay, 
    $fed_tax, 
    $ssn_deduction, 
    $city_tax, 
    $union_dues, 
    $net_pay, 
);