in reply to printf format to blank

The solution is to format it in two passes. In pass 1 you map your data to what you want ie 1,234.00 or '' for a 0 result using sprintf, a commify sub, and some logic. In your printf you simply render this data in the desired columns using %Ns for all data. Perl will happily stringify the formatted numbers. You could also use a format

You might SuperSearch for commify for numerous ways to add commas, but be aware not all of them deal with floats correctly. Something along these lines should do the trick:

printf "%-30s %11ss %7s%% %10s %16s\n", $_, $ela{$_} ? commify( sprintf( "%8.2f", $ela{$_}/$version ) ) + : '', $ela{$_} ? commify( sprintf( "%5.1f", $ela{$_}/$r*100) ) + : '', $ctr{$_} ? commify( sprintf( "%10d", $ctr{$_} ) ) + : '', $ctr{$_} && $ela{$_} ? sprintf( "%8.7f", $ela{$_}/$ctr{$_}/$versio +n ): ''; sub commify { local $_ = reverse shift; /\./g; s/\G(\d{3})(\d)/$1,$2/g; scalar reverse $_ }

You need to allow for potential extra field width from the commas of course.

cheers

tachyon

Replies are listed 'Best First'.
Re^2: printf format to blank
by Lhamo Latso (Scribe) on Sep 07, 2004 at 06:03 UTC
    thanks. It all works great!!!