I am printing some decimal and floating point numbers using printf. For most cases I have data, but sometimes the columns are zero, or the denominator is zero. I have successfully captured these "divide by zero" conditions, but now seek help with the format.

I would like the zeros to be blank in the columns # Calls, and Dur/Call, as in the unaccounted-for line below.

Also, my input locale doesn't put a thousands separator comma for the # Calls column. I tried "use locale", but I'm really at a loss here.

Thanks for the help.

Here is the output:

Response Time Component                Duration     # Calls   Dur/Call
------------------------------- ---------------- ---------- ----------
db file sequential read             8.98s  84.8%       3120  0.0028780
CPU service                         1.25s  11.8%         11  0.1136364
unaccounted-for                     0.35s   3.3%          0  0.0000000
SQL*Net message from client         0.01s   0.1%          5  0.0018424
SQL*Net message to client           0.00s   0.0%          5  0.0000028
------------------------------- ---------------- ---------- ----------
Total response time               10.59s  100.0%
use strict; use warnings; my $DEBUG = (shift @ARGV || 0); my $version = 100; # pre-Oracle9i input data is centi-seconds. my %cid; # cursor id of recursive cursors. my %ela; # $ela{event} contains sum of ela statistics for e +vent my %ctr; # $ctr{event} contains count of events my $sum_ela = 0; # sum of all ela times across events my $r = 0; # response time for database call my $action = "(?:PARSE|EXEC|FETCH|UNMAP|SORT UNMAP)"; while (<>) { if (/^(Oracle9i)/) { $version = 1000000; # Oracle9i input data is micro +-seconds. print "version $1 trace data found\n" if $DEBUG; } if (/^PARSING IN CURSOR #(\d+) len=(\d+) dep=(\d+)/i) { $cid{$1} = $3; print "Cursor #$1 is recursive level $cid{$1}\n" if $DEBUG; } if (/^WAIT #(\d+): nam='([^']*)' ela=\s*(\d+)/i) { if (!defined $cid{$1} || $cid{$1} == 0) { # check dep= value for complete trace input only $ctr{$2}++; $ela{$2} += $3; $sum_ela += $3; } } elsif (/^$action #(\d+):c=(\d+),e=(\d+),p=(\d+),cr=(\d+),cu=(\d+), +mis=(\d+),r=(\d+),dep=(\d+)/i) { if (!defined $cid{$1} || $cid{$1} == 0) { # check dep= value for complete trace input only $ctr{"CPU service"}++; $ela{"CPU service"} += $2; $r += $3; } } } $ela{"unaccounted-for"} = $r - ($ela{"CPU service"} + $sum_ela); $ctr{"unaccounted-for"} = 0; printf "\n%-30s %16s %10s %10s\n", "Response Time Component", "Duration", "# Calls", "Dur/Call"; printf "%30s- %16s %10s %10s\n", "-"x30, "-"x16, "-"x10, "-"x10; printf "%-30s %8.2fs %5.1f%% %10d %8.7f\n", $_, $ela{$_}/$version, $ela{$_}/$r*100, $ctr{$_}, ($ctr{$_} && $ela{$_}/$ctr{$_}/$version ||0) for sort { $ela{$b} <=> $ela{$a} } keys %ela; printf "%30s- %16s %10s %10s\n", "-"x30, "-"x16, "-"x10, "-"x10; printf "%-30s %8.2fs %5.1f%%\n\n", "Total response time", $r/$version +, 100;

In reply to printf format to blank by Lhamo Latso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.