Formatting output with 'printf' in Perl

后端 未结 2 1632
南笙
南笙 2021-01-23 03:14

I\'m trying to format my output to look like it\'s in columns. I\'m trying to use the printf function.

Here\'s what I have:

printf(\"%s %10s %12s %10s\\n         


        
2条回答
  •  醉梦人生
    2021-01-23 04:11

    You need to use the same numbers in the two formats:

    printf("%3s %10s %15s %13s\n", "Qty", "Desc.", "Unit \$", "Total");
    

    and

    printf("%3d %10s %12.2f %10.2f\n", @quantity[$he], @selections[$he], @prices[$he], @prices[$he]*@quantity[$he])
    

    Note that 12.2 means (12 digits + 1 point + 2 digits), which is why I wrote 15 in the first format. The same goes for the 13.


    Also note that you're accessing array elements incorrectly.

    Instead of @quantity[$he] use $quantity[$he]. That is, replace the @ with a $.

提交回复
热议问题