Thank you, I'm glad we were able to get it working for you. I've learned a lot about style from this site too. By the way, I noticed a mistake: I was using too long a length in my substr() calls, so I was treating seconds as minutes and minutes as hours. I've corrected that in the version below.
On making the column widths dynamic, think about what you'll need to do. After getting all the values into your hash tables, you'll need to loop through them by column, finding the length of each value and saving the largest length found somewhere, matched to that column. We can save them as the values of our %db hash, since we just had '1' placeholders there before. So the keys of %db will still be the database names (and column headers), but later the values will become the column widths.
This is a bit complicated, and we need to do it twice, once for the hour report and once for the minute report, so I'll make it a subroutine. I pass the main hash (%h or %m) and the %db hash to it as references. I also pass a ref to the @db array of database names, so the subroutine doesn't have to re-get the keys from %db. Since the top-level keys of the hash are the datetimes, I have to loop through those first, then inside that I loop through the database names, decide which length is longer -- the one already saved for that column, or the length of the current item -- and save that in the database name hash. When it's finished, my main program can get the lengths for each column from the values in %db.
sub set_column_widths {
my $h = shift; # reference to hash table of speeds, keyed
+by datetime, then by database
my $databases = shift; # reference to hash of database names, wher
+e we will set the width values
my $names = shift; # ref to array of database names
# (so we don't need to call keys on $da
+tabases repeatedly)
for my $key (keys %$h){
for my $db (@$names){
my $l = length $h->{$key}{$db}; # get length of this item
+ in the hash table
# set column width to the
+ widest length
$databases->{$db} = $databases->{$db} > $l ? $databases->{
+$db} : $l;
}
}
for my $db (@$names){ # check the width of the database names the
+mselves too
my $l = length $db;
$databases->{$db} = $databases->{$db} > $l ? $databases->{$db}
+ : $l;
}
}
Now I just need to call that before printing each report, like this:
set_column_widths(\%h, \%db, \@db);
Now when it's time to print out the columns, we can get the width and use it in the printf() statements where I previously hard-coded 11. For instance, in this line which prints the database names:
printf "%11s", $_ for (@db);
# replace 11 with $db{$_}, the saved width for this column, and
# stick a space between columns
printf " %$db{$_}s", $_ for (@db);
You'll need to make the same change in the other printf() statement, and then you should have dynamic-width columns. Here's the full script with these changes, in case it's not clear where I made them, plus the fixes for my substr() length mistake:
Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.
|