Author: spadkins
Date: Mon Sep 18 13:06:19 2006
New Revision: 7843

Modified:
   p5ee/trunk/App-Repository/bin/dbget

Log:
fixed column width determination

Modified: p5ee/trunk/App-Repository/bin/dbget
==============================================================================
--- p5ee/trunk/App-Repository/bin/dbget (original)
+++ p5ee/trunk/App-Repository/bin/dbget Mon Sep 18 13:06:19 2006
@@ -58,10 +58,13 @@
 }
 
 sub print_table {
+    &App::sub_entry if ($App::trace);
     my ($rows, $columns, $formats, $options) = @_;
     my ($row, $r, $c, $elem, $format, $len, $f, $heading);
     my (@autoformat);
     my $headings = $options->{headings};
+
+    # compute the number of columns as the max columns of any row
     my $max_columns = 0;
     for ($r = 0; $r <= $#$rows; $r++) {
         $row = $rows->[$r];
@@ -69,16 +72,21 @@
             $max_columns = $#$row + 1;
         }
     }
+
+    # compute automatic sprintf formats
     for ($c = 0; $c < $max_columns; $c++) {
+
         if (! defined $autoformat[$c]) {
             $autoformat[$c] = {
                 max_length => 0,
-                type => 0,        # 0=string, 1=integer, 2=float
+                type => 2,        # 0=string, 1=float, 2=integer
                 min => undef,
                 max => undef,
             };
         }
         $f = $autoformat[$c];
+
+        # set the length of the column by the length of its heading
         $heading = ($headings && $headings->[$c]) ? $headings->[$c] : "";
         if ($heading) { 
             $len = length($heading);
@@ -92,14 +100,15 @@
                 $f->{max_length} = $len;
             }
         }
+
         for ($r = 0; $r <= $#$rows; $r++) {
             $row = $rows->[$r];
             if ($c <= $#$row && defined $row->[$c]) {
                 $elem = $row->[$c];
                 $len = length($elem);
-                if ($elem =~ /^-?[0-9]*\.[0-9]+$/) {
+                if ($elem =~ /^-?[0-9]*\.[0-9]+$/) {  # float
                     $len = length(sprintf("%.2f",$elem));
-                    $f->{type} = 2;
+                    $f->{type} = 1 if ($f->{type} > 1);
                     if (!defined $f->{min} || $elem < $f->{min}) {
                         $f->{min} = $elem;
                     }
@@ -107,8 +116,7 @@
                         $f->{max} = $elem;
                     }
                 }
-                elsif ($elem =~ /^-?[0-9]+$/) {
-                    $f->{type} = 1 if ($f->{type} < 1);
+                elsif ($elem =~ /^-?[0-9]+$/) { # integer
                     if (!defined $f->{min} || $elem < $f->{min}) {
                         $f->{min} = $elem;
                     }
@@ -116,6 +124,9 @@
                         $f->{max} = $elem;
                     }
                 }
+                else {
+                    $f->{type} = 0;
+                }
                 $f->{max_length} = $len if ($len > $f->{max_length});
             }
         }
@@ -137,16 +148,18 @@
         }
         print "\n";
     }
+    &App::sub_exit() if ($App::trace);
 }
 
 sub determine_sprintf_fmt {
+    &App::sub_entry if ($App::trace);
     my ($f) = @_;
     my ($width, $int_len, $fract_len);
-    if ($f->{type} == 2) {     # float
+    if ($f->{type} == 1) {     # float
         $f->{title_fmt} = "%$f->{max_length}.$f->{max_length}s";
         $f->{fmt} = "%$f->{max_length}.2f";
     }
-    elsif ($f->{type} == 1) {  # integer
+    elsif ($f->{type} == 2) {  # integer
         $f->{title_fmt} = "%$f->{max_length}.$f->{max_length}s";
         $f->{fmt} = "%$f->{max_length}d";
     }
@@ -154,6 +167,7 @@
         $f->{title_fmt} = "%-$f->{max_length}.$f->{max_length}s";
         $f->{fmt} = "%-$f->{max_length}s";
     }
+    &App::sub_exit($f->{fmt}) if ($App::trace);
 }
 
 exit (0);

Reply via email to