Hi, your problem description is not very clear. I think you are saying that you want to be able to handle arrays of varying lengths with a single sprintf statement. If so, you can always use the result of an expression as the format:
Output:use strict; use warnings; my @slow_day = ( 4, 1..4 ); my @busy_day = ( 12, 1..12 ); for my $report ( \@slow_day, \@busy_day ) { my ( $total, @sales ) = @{ $report }; my $fmt = 'Total: $%2d Sales:' . ' $%.02f' x scalar(@sales); print sprintf($fmt, $total, @sales), "\n"; } __END__
Similarly you can examine the list and use the longest element length as a padding value, etc.$ perl 1223893.pl Total: $ 4 Sales: $1.00 $2.00 $3.00 $4.00 Total: $12 Sales: $1.00 $2.00 $3.00 $4.00 $5.00 $6.00 $7.00 $8.00 $9.0 +0 $10.00 $11.00 $12.00
As to your second question: What happens when perl ... "runs out" of parameters (there are more variables to print than parameters in the format) ... Well, what happens when you try it? You can just use a one-liner for that:
$ perl -Mstrict -wE 'say sprintf("%s", qw/two strings/);'
Hope this helps!
In reply to Re: sprintf and arrays of unknown (in advance) length
by 1nickt
in thread sprintf and arrays of unknown (in advance) length
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |