comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Controlling width of strings during Put operation?
Date: Sun, 28 Dec 2003 22:38:13 -0500
Date: 2003-12-28T22:38:13-05:00	[thread overview]
Message-ID: <UtadnTjRGJY7PHKi4p2dnA@comcast.com> (raw)
In-Reply-To: <MPG.1a5961997d61e67a989688@news.sover.net>

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




  reply	other threads:[~2003-12-29  3:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-29  2:57 Controlling width of strings during Put operation? Peter C. Chapin
2003-12-29  3:38 ` Robert I. Eachus [this message]
2003-12-29  4:19   ` Peter C. Chapin
replies disabled

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