and, later, some moron, usually me, decides that if $root_beer is zero and/or an empty string the printed field should maintain its width but be blank.printf( "A float: %12.4f$/", $root_beer );
A common place issue and my reflex is to do the same things I did in C decades ago: Use a variable as a conversion specifier ($fmt="%12s") or use sprintf then printf.
I am asking for people's best practices on printing blank fields for values like zero, undef and empty string in places where the conversion specifier would expect a numeric and other like issues.
Update: The nature of the replies I've gotten aren't what I hoped for. As a programming problem I'd like to hear the voice of experience. But I'd also like to hear about the quirks (impedance mismatch) of what people want to read in their reports versus what is trivial to output. ( Re-Update: Much later and I've found fizbin's code useful.)
My best solution to date:
Which is least invasive of the printf.my $fmt = fmt_num_or_blank( $width, $data); printf( "A float: $fmt$/", $root_beer );
My best conception is something like:
but at best I could only weakly implement that.# Mnemonic: N nothing for nought nor null printf( "A float: %N12.4f$/", $root_beer );
Over the years there has been a lot of hate mail for printf formatting as being messy and error prone. I think it just reflects the problem domain. Are there other output massagers to look at?
Be well,
rir
In reply to Blankety-Blank Blanks and Zeros, etc. by rir
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |