Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stupid? format question

10 views
Skip to first unread message

Bob Felts

unread,
Jan 23, 2009, 10:08:00 AM1/23/09
to
I want to output tabbed data for later input to Excel or FileMaker or...

I can obviously do that with
(format t "~a---tab goes here---~a~%" field1 field2)

In E-macs I can type C-q Tab to insert a tab.

But I miss the C-style ability to backquote special characters and I
haven't found any equivalent in Lisp. ~t outputs spaces. Have I just
overlooked it?


Alexander Lehmann

unread,
Jan 23, 2009, 10:14:38 AM1/23/09
to
Hi Bob,

Bob Felts wrote:
> I want to output tabbed data for later input to Excel or FileMaker or...

does this help?

<http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/f104f29d379f3bf8>

Marco Antoniotti

unread,
Jan 23, 2009, 11:00:38 AM1/23/09
to

(format t "|~C|~%" #\Tab)

outputs a single tab. Have also a look at the ~T directive.

Cheers
--
Marco

Raffael Cavallaro

unread,
Jan 23, 2009, 11:21:37 AM1/23/09
to
On 2009-01-23 10:08:00 -0500, wr...@stablecross.com (Bob Felts) said:

> But I miss the C-style ability to backquote special characters and I
> haven't found any equivalent in Lisp. ~t outputs spaces. Have I just
> overlooked it?

(string #\TAB) ?
--
Raffael Cavallaro, Ph.D.

Alberto Riva

unread,
Jan 23, 2009, 11:47:34 AM1/23/09
to

And if you have multiple fields you want to print with tabs between them
you can do the following:

(let ((fmt (format nil "~~{~~a~~^~a~~}" #\tab)))
(format nil fmt (list field1 field2 field3 ...))

Alberto

Bob Felts

unread,
Jan 23, 2009, 4:21:55 PM1/23/09
to
Alexander Lehmann <lehm...@in.tum.de> wrote:

Not really, for two reasons. First, it's CSV and I want tab-delimited
fields. Second, I've already written the code. It was easy.

I'm just curious if Lisp has the ability to embed human-readable control
characters in a format string. I can embed a tab character, but if I
have multiple sequential tabs, I can't tell in Emacs how many I have by
visual inspection. That is, I have to eyeball a variable number of
spaces and convert them to the equivalent number of tabs, instead of
simply counting \t\t\t.

I can also use ~c along with an argument of #\Tab, but that puts a layer
of indirection between the format string and the output.

Alberto kindly provided this example:

(let ((fmt (format nil "~~{~~a~~^~a~~}" #\tab)))
(format nil fmt (list field1 field2 field3 ...))

It's informative and on the one hand I think it's an interesting
technique. On the other hand, it offends my sense of aesthetics.

Absent something like C's \t (or \n, or \g, or ...), I was hoping that
perhaps there as a reader macro, or something, that could be used inside
of a string to specify a control character.

Zach Beane

unread,
Jan 23, 2009, 4:25:47 PM1/23/09
to
wr...@stablecross.com (Bob Felts) writes:

> Absent something like C's \t (or \n, or \g, or ...), I was hoping that
> perhaps there as a reader macro, or something, that could be used inside
> of a string to specify a control character.

CL-INTERPOL does something like that.

Zach

GP lisper

unread,
Jan 23, 2009, 6:22:50 PM1/23/09
to
On Fri, 23 Jan 2009 16:21:55 -0500, <wr...@stablecross.com> wrote:
>
> I'm just curious if Lisp has the ability to embed human-readable control
> characters in a format string. I can embed a tab character, but if I
> have multiple sequential tabs, I can't tell in Emacs how many I have by
> visual inspection.

Try hexl-mode

After 22 versions spanning a fair number of years, "can't" rarely
exists in Emacs (and if you give a hacker a hint...).

Thomas A. Russ

unread,
Jan 23, 2009, 6:18:54 PM1/23/09
to
wr...@stablecross.com (Bob Felts) writes:

> I want to output tabbed data for later input to Excel or FileMaker or...

...


> In E-macs I can type C-q Tab to insert a tab.
>
> But I miss the C-style ability to backquote special characters and I
> haven't found any equivalent in Lisp. ~t outputs spaces. Have I just
> overlooked it?

Well, the main reason is that lisp strings take their characters
directly and don't really need quoting (except for the string delimiter
" and the escape characer \). So there isn't any mechanism in Lisp for
quoting characters that don't actually need it.

I suspect that the major difference between lisp and the C-style
languages is that lisp allows you to have line breaks inside your
strings directly. So for example

"This is a long string that
takes up all of three
separate lines"

is a legal string constant in Lisp, but not in C, C++, Java or most
other languages that I can think of. So in languages that don't allow
the line breaks into strings, there is a need to quote them. And if you
are quoting \n and \r, it is only a small leap to add \t, especially for
cases where it might be indistinguishable visually from space.

But since lisp didn't need to have special character quotation in the
first place, it just wasn't added to the language.

(But I do agree that it would sometimes be convenient.)

--
Thomas A. Russ, USC/Information Sciences Institute

Bob Felts

unread,
Jan 23, 2009, 8:03:01 PM1/23/09
to
Zach Beane <xa...@xach.com> wrote:

When will I learn to go to Edi's site first? ;-)
Thanks, Zach.

Bob Felts

unread,
Jan 23, 2009, 8:03:01 PM1/23/09
to
GP lisper <spam...@CloudDancer.com> wrote:

Thanks for the pointer. I'll check it out.

Pascal J. Bourguignon

unread,
Jan 23, 2009, 8:30:48 PM1/23/09
to

And trivial to implement as a reader macro on #\" if you need it.
(Several examples have already been posted in cll).
--
__Pascal Bourguignon__

William James

unread,
Jan 23, 2009, 9:23:07 PM1/23/09
to
Alberto Riva wrote:

Ruby:

puts ["foo","bar","barely"].join "\t"
foo bar barely
==>nil

William James

unread,
Jan 23, 2009, 9:41:01 PM1/23/09
to
Thomas A. Russ wrote:

> wr...@stablecross.com (Bob Felts) writes:
>
> > I want to output tabbed data for later input to Excel or FileMaker
> > or...
> ...
> > In E-macs I can type C-q Tab to insert a tab.
> >
> > But I miss the C-style ability to backquote special characters and I
> > haven't found any equivalent in Lisp. ~t outputs spaces. Have I
> > just overlooked it?
>
> Well, the main reason is that lisp strings take their characters
> directly and don't really need quoting (except for the string
> delimiter " and the escape characer \). So there isn't any mechanism
> in Lisp for quoting characters that don't actually need it.
>
> I suspect that the major difference between lisp and the C-style
> languages is that lisp allows you to have line breaks inside your
> strings directly. So for example
>
> "This is a long string that
> takes up all of three
> separate lines"
>
> is a legal string constant in Lisp, but not in C, C++, Java or most
> other languages that I can think of.

It's legal in Ruby and in OCaml, but they allow quoting.
Does this string contain a tab?

"mewler lisper"

Does this string contain a tab?

"mewler\tlisper"

See the difference?

Pascal J. Bourguignon

unread,
Jan 23, 2009, 9:52:56 PM1/23/09
to
"William James" <w_a_...@yahoo.com> writes:
> Does this string contain a tab?
>
> "mewler lisper"
>
> Does this string contain a tab?
>
> "mewler\tlisper"
>
> See the difference?

TAB is not a character, it's a control code.
It has nothing to do in text files or in strings.

You may write:

(send-string device "mewler")
(send-control device 9)
(send-string device "lisper")

if you will.

--
__Pascal Bourguignon__

Marco Antoniotti

unread,
Jan 24, 2009, 4:05:35 AM1/24/09
to

Not the same as Alberto's... Besides the shortest CL program is

cl-prompt> (tabify '("foo" "bar" "baz"))
foo bar baz


Cheers
--
Marco
www.european-lisp-symposium.org


Mirko

unread,
Jan 24, 2009, 8:06:52 PM1/24/09
to

Can I add my own stupid format question? I will just assume the
answer is yes. Here it goes:

How can I print "abc" (i.e. the string `abc' enclosed in quotes).
From this discussion I found one way:

(format t "~aabc~a~%" (string #\") (string #\"))

Any way of quoting `"' in the format string itself?

Thank you,

Mirko

Zach Beane

unread,
Jan 24, 2009, 8:10:59 PM1/24/09
to
Mirko <Mirko....@gmail.com> writes:

In a string, you can precede " with \ to include " in the string:

(format t "\"abc\"")

You could also do this:

(format t "~S" "abc")

That's a little different, though.

Zach

Pascal J. Bourguignon

unread,
Jan 24, 2009, 8:13:18 PM1/24/09
to
Zach Beane <xa...@xach.com> writes:

That's a little better I'd say:


C/USER[2]> (progn (format t "\"~A\"~%" "abc\"def")
(format t "~S~%" "abc\"def")
(values))
"abc"def"
"abc\"def"

C/USER[3]>


--
__Pascal Bourguignon__

Mirko

unread,
Jan 24, 2009, 8:53:27 PM1/24/09
to
On Jan 24, 8:13 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Zach Beane <x...@xach.com> writes:

I am sure you two were composing this answer even before I hit the
`send' key.

Thanks :-)

0 new messages