Hi Luca, On Tue, 3 Jan 2017 10:35:46 +0100 Luca Ferrari <fluca1...@infinito.it> wrote:
> Hi all, > 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? > Perhaps try looking at this page about generating text in Perl - http://perl-begin.org/uses/text-generation/ (note that Perl-Begin is a site which I maintain). This page on my personal website, which is not Perl-specific, but which may have some Perl bias may also be of interest - http://www.shlomifish.org/open-source/resources/text-processing-tools/ . Good luck! -- Shlomi > Thanks, > Luca > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/