I've a program that needs to print some fields formatted in different ways according to some conditions. The solution I come up is working, but I'm looking for a suggestion for something more elegant.
What I do is something like the following:
print sprintf $formats[ $condition ], @fields;
where $condition is the condition used to select a sprintf format string out of an array (@formats) that contains something like:
my @formats = (
qw( %09d %-1s %03d ... )
, qw(%-4s %1s %09d %1s %-150s %-4s %011d)
, ...
);
Now, while this approach is working really fine, it is a little hard to decode, especially considering that I've got some formats with 50+ fields. I don't believe that using Perl formats is a solution, it will provide a quite longer configuration (consider I've got even fields specified as "-100%s"!).
Any suggestion to get a more readable code?