comp.lang.ada
 help / color / mirror / Atom feed
* Controlling width of strings during Put operation?
@ 2003-12-29  2:57 Peter C. Chapin
  2003-12-29  3:38 ` Robert I. Eachus
  0 siblings, 1 reply; 3+ messages in thread
From: Peter C. Chapin @ 2003-12-29  2:57 UTC (permalink / raw)



Hello!

I'm trying to print out a nice table of results using the standard IO 
packages. I can print the table rows fairly nicely. I see that the Put 
procedure in Integer_IO takes a Width parameter that is defaulted to 
Num'Width (Num being the type used to instantiate Integer_IO). That is 
fine. In fact, it's good.

My issue is: how do I best print the table headers? I could certainly 
play around with spaces until I got good looking results but that seems 
fragile and inelegant. I'd rather set a field width for each header 
based on the Num'Width of the numeric type in the column below that 
header. That way if I change the type of one of the columns, the headers 
have a prayer of still lining up without me fiddling with them again. My 
problem is that I don't see a Put procedure that allows me to specify a 
field width when I print a string. Do I have to "manually" prepare my 
own header strings with the appropriate number of padding spaces? Is 
there an easy way to do that?

Thanks!

Peter



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Controlling width of strings during Put operation?
  2003-12-29  2:57 Controlling width of strings during Put operation? Peter C. Chapin
@ 2003-12-29  3:38 ` Robert I. Eachus
  2003-12-29  4:19   ` Peter C. Chapin
  0 siblings, 1 reply; 3+ messages in thread
From: Robert I. Eachus @ 2003-12-29  3:38 UTC (permalink / raw)


Peter C. Chapin wrote:

> My issue is: how do I best print the table headers? I could certainly 
> play around with spaces until I got good looking results but that seems 
> fragile and inelegant. I'd rather set a field width for each header 
> based on the Num'Width of the numeric type in the column below that 
> header. That way if I change the type of one of the columns, the headers 
> have a prayer of still lining up without me fiddling with them again. My 
> problem is that I don't see a Put procedure that allows me to specify a 
> field width when I print a string. Do I have to "manually" prepare my 
> own header strings with the appropriate number of padding spaces? Is 
> there an easy way to do that?

Yes. I would use Ada.Strings.Fixed.Trim (see 
http://www.adaic.org/standards/95lrm/html/RM-A-4-3.html)

with Ada.Strings.Fixed;
with Ada.Text_IO;
...
function Pad(Header: in String; Size: Positive := Integer'Width) return 
String is
   use Ada.Strings, Ada.Strings.Fixed;
   Result: String(1..Size);
begin
   Move(Source => Header,
        Target => Result,
        Drop => Right,
        Justify => Center,
        Pad => Space);
    -- or of course, whatever values you want.
    return Result;
end Pad;

Now of course you just write your header line something like:

Ada.Text_IO.Put_Line( Pad("Column 1") & Pad("Column 2") & ...);

I often think that there should be a function form of Move, in 
Ada.Strings.Fixed.  But since it is usually much more convenient to use 
the above technique to reset some of the defaults anyway, I don't get 
too upset about it.

-- 
                                           Robert I. Eachus

"The war on terror is a different kind of war, waged capture by capture, 
cell by cell, and victory by victory. Our security is assured by our 
perseverance and by our sure belief in the success of liberty." -- 
George W. Bush




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Controlling width of strings during Put operation?
  2003-12-29  3:38 ` Robert I. Eachus
@ 2003-12-29  4:19   ` Peter C. Chapin
  0 siblings, 0 replies; 3+ messages in thread
From: Peter C. Chapin @ 2003-12-29  4:19 UTC (permalink / raw)


In article <UtadnTjRGJY7PHKi4p2dnA@comcast.com>, rieachus@comcast.net 
says...

> Now of course you just write your header line something like:
> 
> Ada.Text_IO.Put_Line( Pad("Column 1") & Pad("Column 2") & ...);

Thanks! That looks promising.

Peter



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-12-29  4:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-29  2:57 Controlling width of strings during Put operation? Peter C. Chapin
2003-12-29  3:38 ` Robert I. Eachus
2003-12-29  4:19   ` Peter C. Chapin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox