2012-02-08 86 views
1

如何使用printf打印此示例輸出?Perl printf問題

****************************************************************************** 
** 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

據我所知,這是一個已經改變很少在過去的二十年裏,所以幾乎任何你找一個特徵在網上應該給你有用的幫助。

+1

對於一個完整的參考:http://perldoc.perl.org/perlform.html – flesk 2012-02-08 07:16:44

+2

我聽見Perl6 ::表(HTTP://search.cpan。 org/perldoc?Perl6 :: Form)(用於Perl5的Perl6風格表單)比Perl5的內置格式要好得多。因人而異。 – ikegami 2012-02-08 08:00:16

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, 
);