From: David Bakhash
Subject: help with FORMAT
Date: 
Message-ID: <m3ithuqzqk.fsf@alum.mit.edu>
Hi,

here's a simple question:

What's the nicest way to achieve the following using only one call to
FORMAT?

 (let ((length 16)   ; length is some integer
       (number 999)) ; some number to be printed out zero-padded
   (format t (format nil "~~~D,'0D" length) number))

Assume that at compile-time I only know that LENGTH is a positive
integer, and same for number.

thanks,
dave

From: Kent M Pitman
Subject: Re: help with FORMAT
Date: 
Message-ID: <sfwhexet62x.fsf@world.std.com>
David Bakhash <·····@alum.mit.edu> writes:

> Hi,
> 
> here's a simple question:
> 
> What's the nicest way to achieve the following using only one call to
> FORMAT?
> 
>  (let ((length 16)   ; length is some integer
>        (number 999)) ; some number to be printed out zero-padded
>    (format t (format nil "~~~D,'0D" length) number))
> 
> Assume that at compile-time I only know that LENGTH is a positive
> integer, and same for number.

You want:

 (let ((length 16) (number 999))
   (format t "~V,'0D" length number))

"V" is not a format op.  It is a letter that can be used in place of any
parameter value to say "get the corresponding parameter from the arguments".
"V" may be used more than once if more than one parameter needs to be 
dealt with this way.

 (format t "~V,VD" 5 #\x 47)
 xxx47
From: Tim Bradshaw
Subject: Re: help with FORMAT
Date: 
Message-ID: <nkjzob6jb8w.fsf@tfeb.org>
David Bakhash <·····@alum.mit.edu> writes:

> 
>  (let ((length 16)   ; length is some integer
>        (number 999)) ; some number to be printed out zero-padded
>    (format t (format nil "~~~D,'0D" length) number))
> 

Can you not use (format t "~V,'0D" length number) ?

--tim
From: David Bakhash
Subject: Re: help with FORMAT
Date: 
Message-ID: <m3d782z3ka.fsf@alum.mit.edu>
hey,

 >> (format t (format nil "~~~D,'0D" length) number))

 tim> Can you not use (format t "~V,'0D" length number) ?

Just didn't know about that.

Actually, FORMAT is something that I've, for some odd reason, NEVER
cared or wanted to learn.  I don't know what it is about it.  It
doesn't strike me as being so different from anything else.  But I
think, over time, I've learned enough of it to say I know it okay.
But that's purely because I've been at it for so long.  I seldom use
FORMAT, and when I do, I try to use it sparingly.

In this case, I'm using it for a protocol, and most of the time, I try 
to use the Lisp reader and printer to read and write directly, without 
worrying about FORMAT.  But this doesn't always work -- especially
when client programs don't like reading S-expressions.

Thanks for the help.

dave

p.s. is there a good FORMAT tutorial out there?  I've never seen one.
From: Tim Bradshaw
Subject: Re: help with FORMAT
Date: 
Message-ID: <nkjpuc1rh6c.fsf@tfeb.org>
David Bakhash <·····@alum.mit.edu> writes:

> 
> Actually, FORMAT is something that I've, for some odd reason, NEVER
> cared or wanted to learn.  I don't know what it is about it.  It
> doesn't strike me as being so different from anything else.  But I
> think, over time, I've learned enough of it to say I know it okay.
> But that's purely because I've been at it for so long.  I seldom use
> FORMAT, and when I do, I try to use it sparingly.


Yeah.  I went for years without using FORMAT in any non-trivial way.
But quite suddenly I started doing all sorts of things like this and
iteration/conditionals &c &c.  I think it might have been perl-envy -
not enough line-noise in my programs.

> 
> p.s. is there a good FORMAT tutorial out there?  I've never seen one.

Neither have I.

--tim
From: Kent M Pitman
Subject: Re: help with FORMAT
Date: 
Message-ID: <sfw8zipvkci.fsf@world.std.com>
David Bakhash <·····@alum.mit.edu> writes:

> p.s. is there a good FORMAT tutorial out there?  I've never seen one.

CLHS 22.3 Formatted Output

;-)
From: Frank A. Adrian
Subject: Re: help with FORMAT
Date: 
Message-ID: <PNzX6.624$Qp2.553067@news.uswest.net>
"David Bakhash" <·····@alum.mit.edu> wrote in message
···················@alum.mit.edu...
> But this doesn't always work -- especially
> when client programs don't like reading S-expressions.

Bloody inconvenient and unfriendly of them, I'd say...

faa