[VPM] Printing one line over several lines

Peter Scott peter at PSDT.com
Wed Mar 26 21:15:01 CST 2003


At 03:56 PM 3/26/03 -0800, Carl B. Constantine wrote:
>I'm working on improving the perl code for the program I was talking
>about at the last meeting. I'm not quite ready to put it out for all to
>read yet as I have to change a bunch of data to protect people's privacy
>and I just haven't gotten around to doing that.
>
>One of the things my boss wants me to do is rework the print queue
>headings. Here are the gory details:
>
>I have an array that contains a list of all of our printers. Some of the
>printer names (print queue names really) are fairly long. For example:
>tp-colour-tr, where as others are relatively short, ie: tp.
>
>When I print the report out for each prof (using some ugly centering
>code to make sure everything lines up properly), the header looks like
>this:
>
>Page Rate ==>   (0.07)    (0.00)    ( 0.07)    ( 
>0.00)    (   0.50)    (      0.50)    (      0.00)
>Student 
>Login       rp        tp    rp-test    tp-test    rp-colour 
>rp-colour-tr    tp-colour-tr    Total
>=======================================================================================================================
>
>Now, what my boss would like is that for print queue names that have a
>hyphan in them, print the queue name over multiple lines while still
>lining things up like so:
>
>Page Rate ==>   (0.07)    (0.00)    (0.07)    (0.00)    ( 0.50)    ( 
>0.50)    ( 0.00)
>Student 
>Login       rp        tp       rp-       tp-        rp-        rp-        tp-
>                                       test      test     colour 
> colour    colour-
> 
>tr-        tr-     Total
>===============================================================================================
>
>
>
>the idea being to try and fit the report into 80 columns as much as
>possible. Quit frankly that header could be slightly different, like say
>the rp & tp and the test printers down a line or two, but the principle
>is the same.

Yeah, that's pretty gross, and the right justification sucks.

I don't have time to test this, but something like this perhaps:

my @split_names = map split_name($_), @allPrinters;
print "Page Rate ==> ", map(col_fmt($_, -1, 
page_rate($allPrinters[$_])) => 0 .. $#allPrinters), "\n";
for (my $line = 0; grep $split_names[$_][$line] => 0..$#allPrinters;
      $line++) {
   my $begin = $line ? "              "
                     : "Student Login ";
   print $begin, map(col_fmt($_, $line, $split_names[$line]) => 0 .. 
$#allPrinters, "\n";
}

sub page_rate {
   sprintf "(%.2f)", $printCost{shift()}/100;
}

sub col_fmt {
   my ($prt, $line, $text) = @_;
   my $width = $line >= 0 ? max($MINWIDTH, 
length($split_names[$prt][$line]||'')) : $MINWIDTH;
   sprintf " %-*s", $width, $text;
}

sub split_name {
   [ shift =~ /(.*-?)/g ];
}


>right now, when I'm printing this header, I have really ugly code that
>looks like this:
>
>print "Page Rate ==>\t";
>   foreach $queue (@allPrinters) {
>     if (exists $profHash{$leftOver}{$queue}) {
>       $len = length($queue);
>       if ($len <= $rateLen) {
>         printf "(%2.2f)    ",$printCost{$queue}/100;
>       } else {
>         $len -= 2; # subtract 2 from the total length to take the ( & 
> ) into account when printing
>         printf "(%".$len.".2f)    ",$printCost{$queue}/100;
>       }
>     }
>   }
>   print "\n";
>   print "Student Login\t";
>   my $count = 0;
>   my $length = 0;
>   foreach $queue (@allPrinters) {
>     if (exists $profHash{$leftOver}{$queue}) {
>       $len = length($queue);
>       if ($len < $rateLen) {
>         printf "%".$rateLen."s    ",$queue;
>   $length += $rateLen;
>       } else {
>         printf "%".$len."s    ",$queue;
>   $length += $len;
>       }
>       $count++;
>     }
>   }
>   print "Total\n";
>   $length += ($count * 6) + 13 + 5; # this hack really needs to be fixed
>   print "=" x $length;              # it doesn't work right anyway.
>   print "\n\n";
>
>any ideas, help are greatly appreciated.
>
>--
>Carl B. Constantine         University of Victoria
>Programmer Analyst          http://www.csc.uvic.ca
>UNIX System Administrator   Victoria, BC, Canada
>cconstan at csc.uvic.ca        ELW A220, 721-8753

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/




More information about the Victoria-pm mailing list