[clisp-list] Newbie: splitting on tabulation

Hello,
when I run the LISP program below
#!/usr/bin/clisp -C
(DEFPACKAGE "REGEXP-TEST" (:use "LISP" "REGEXP"))
(IN-PACKAGE "REGEXP-TEST")
(with-open-file (f "test.txt")
(with-loop-split (s f "\t")
(print s)))
on
Hello world
Hello goodbye
Hello Dolly
where each line contains a word, a tab, and another word
I get
("Hello world")
("Hello goodbye")
("Hello Dolly")
How do you specify a tab in with-loop-split?
Many thanks.
phiroc

Thread view

Hello,
when I run the LISP program below
#!/usr/bin/clisp -C
(DEFPACKAGE "REGEXP-TEST" (:use "LISP" "REGEXP"))
(IN-PACKAGE "REGEXP-TEST")
(with-open-file (f "test.txt")
(with-loop-split (s f "\t")
(print s)))
on
Hello world
Hello goodbye
Hello Dolly
where each line contains a word, a tab, and another word
I get
("Hello world")
("Hello goodbye")
("Hello Dolly")
How do you specify a tab in with-loop-split?
Many thanks.
phiroc

phiroc@... writes:
> (DEFPACKAGE "REGEXP-TEST" (:use "LISP" "REGEXP"))
> (IN-PACKAGE "REGEXP-TEST")
> (with-open-file (f "test.txt")
> (with-loop-split (s f "\t")
> (print s)))
> How do you specify a tab in with-loop-split?
Read again the specifications of Common Lisp. You're not in C anymore.
http://www.lispworks.com/documentation/HyperSpec/Body/02_de.htm
If a single escape character is seen, the single escape character
is discarded, the next character is accumulated, and accumulation
continues.
Try:
(princ "\t")
TAB may mean nothing on some systems where Common Lisp works.
TAB is a control code in the ASCII (and therefore ISO-8859 and
therefore Unicode) character encoding, but on systems where these
encodings are not used, TAB may well not exist, and nowadays, very few
devices still exist interpreting such a control code. It was used to
control the movement of the carriage on a teletype. When was the last
time you used a teletype?
Anyways, you're still lucky. In the implementations that have such a
control code, its mapped to a Common Lisp character with a standard
name: #\tab
And clisp is one of them.
So you can insert such a control code in a Common Lisp or clisp
program like this:
(format nil "before TAB >~C< after TAB" #\tab)
If you want a string with only a TAB control code in it:
(defparameter *tab* (format nil "~C" #\tab))
; or:
(defparameter *tab* (make-string 1 :initial-element #\tab))
Note that when you want to split a string on a single character, you
can use split-sequence (from the separately provided, de-facto
standard split-sequence package):
(split-sequence:split-sequence #\tab some-string)
Finally, I like to write portable programs, so I wouldn't use #\tab
without first testing if it exists:
(defun tab-exists-p ()
(ignore-errors (read-from-string "#\tab")))
Then I could write:
(defparameter *tab* (or (tab-exists-p)
(make-my-own-object-representing-something-like-tab-to-do-whatever-I-might-want-to-do-with-such-a-thing)))
--
__Pascal Bourguignon__
http://www.informatimago.comhttp://pjb.ogamita.org

Thanks for the information.
(format nil "~C" #\tab) did the trick.
phiroc
#!/usr/bin/clisp -C
(DEFPACKAGE "REGEXP-TEST" (:use "LISP" "REGEXP"))
(IN-PACKAGE "REGEXP-TEST")
(with-open-file (f "test.txt")
(with-loop-split (s f (format nil "~C" #\tab))
(print s)))
Quoting Pascal Bourguignon <pjb@...>:
> phiroc@... writes:
> > (DEFPACKAGE "REGEXP-TEST" (:use "LISP" "REGEXP"))
> > (IN-PACKAGE "REGEXP-TEST")
> > (with-open-file (f "test.txt")
> > (with-loop-split (s f "\t")
> > (print s)))
> > How do you specify a tab in with-loop-split?
>
> Read again the specifications of Common Lisp. You're not in C anymore.
> http://www.lispworks.com/documentation/HyperSpec/Body/02_de.htm
>
> If a single escape character is seen, the single escape character
> is discarded, the next character is accumulated, and accumulation
> continues.
>
> Try:
>
> (princ "\t")
>
>
> TAB may mean nothing on some systems where Common Lisp works.
>
> TAB is a control code in the ASCII (and therefore ISO-8859 and
> therefore Unicode) character encoding, but on systems where these
> encodings are not used, TAB may well not exist, and nowadays, very few
> devices still exist interpreting such a control code. It was used to
> control the movement of the carriage on a teletype. When was the last
> time you used a teletype?
>
>
> Anyways, you're still lucky. In the implementations that have such a
> control code, its mapped to a Common Lisp character with a standard
> name: #\tab
>
> And clisp is one of them.
>
>
> So you can insert such a control code in a Common Lisp or clisp
> program like this:
>
> (format nil "before TAB >~C< after TAB" #\tab)
>
> If you want a string with only a TAB control code in it:
>
> (defparameter *tab* (format nil "~C" #\tab))
> ; or:
> (defparameter *tab* (make-string 1 :initial-element #\tab))
>
>
>
> Note that when you want to split a string on a single character, you
> can use split-sequence (from the separately provided, de-facto
> standard split-sequence package):
>
> (split-sequence:split-sequence #\tab some-string)
>
>
>
>
> Finally, I like to write portable programs, so I wouldn't use #\tab
> without first testing if it exists:
>
> (defun tab-exists-p ()
> (ignore-errors (read-from-string "#\tab")))
>
> Then I could write:
>
> (defparameter *tab* (or (tab-exists-p)
>
>
(make-my-own-object-representing-something-like-tab-to-do-whatever-I-might-want-to-do-with-such-a-thing)))
>
>
> --
> __Pascal Bourguignon__
> http://www.informatimago.com
> http://pjb.ogamita.org
>