Hi
I just started using lisp, and am quite amazed by its power. However, I
stumbled onto a problem that just totally stumped me.
I want to open a series of files, generically calle screen.1, screen.2,
screen.3, etc... up to screen.90
I figured that I could use a function that returns a string with the
appropriate name. On each call, it changes the last two elements in the
string vector. However,
(setf (elt file-name 8) 3) gives an error because 3 isn't a character.
I tried (setf (elt test 50) `#\,current-num), where current-num is the
function that returns the incremented number, and (setf (elt test 50)
`#\,(current-num))
I've looked for a function that converts integers to character - sadly
enough, integer-to-char only exists in emacs.
write-string doesn't take integers either.
I've looked through 2 tutorial, "Lisp, the Language" by Steele, and even
the actual ANSI standard... I just don't know where else to search
anylonger. Can someone help please?
Derek
I think what you want is called (rather crypticly IMHO) code-char.
E.
In article <······················@news20.bellglobal.com>, Derek Hans
<·········@gmx.net> wrote:
> Hi
> I just started using lisp, and am quite amazed by its power. However, I
> stumbled onto a problem that just totally stumped me.
>
> I want to open a series of files, generically calle screen.1, screen.2,
> screen.3, etc... up to screen.90
>
> I figured that I could use a function that returns a string with the
> appropriate name. On each call, it changes the last two elements in the
> string vector. However,
> (setf (elt file-name 8) 3) gives an error because 3 isn't a character.
> I tried (setf (elt test 50) `#\,current-num), where current-num is the
> function that returns the incremented number, and (setf (elt test 50)
> `#\,(current-num))
> I've looked for a function that converts integers to character - sadly
> enough, integer-to-char only exists in emacs.
> write-string doesn't take integers either.
> I've looked through 2 tutorial, "Lisp, the Language" by Steele, and even
> the actual ANSI standard... I just don't know where else to search
> anylonger. Can someone help please?
>
> Derek
···@jpl.nasa.gov (Erann Gat) writes:
> I think what you want is called (rather crypticly IMHO) code-char.
No, I think Derek wants format.
For instance,
(let ((filename (format nil "screen.~D" integer)))....
code-char would lead one to doing something like
(let ((character (code-char (+ integer (char-code #\0)))))...
which only works with some text encodings (and would need to be
extended to numbers > 9).
While it might be slightly faster to do this by just changing the
last two characters of the string, it's not worth the hassle.
--
Alan Shutko <···@acm.org> - I am the rocks.
Looking for a developer in St. Louis? http://web.springies.com/~ats/
Oh, I can 'C' clearly now, my brain is gone...
Alan Shutko wrote:
> No, I think Derek wants format.
>
> For instance,
>
> (let ((filename (format nil "screen.~D" integer)))....
>
:) That is brilliant! Precisely what I need, and a lot shorter than I
expected. Thanks!
Alan Shutko <···@acm.org> writes:
> ···@jpl.nasa.gov (Erann Gat) writes:
>
>> I think what you want is called (rather crypticly IMHO) code-char.
>
> No, I think Derek wants format.
>
> For instance,
>
> (let ((filename (format nil "screen.~D" integer)))....
>
> code-char would lead one to doing something like
>
> (let ((character (code-char (+ integer (char-code #\0)))))...
>
> which only works with some text encodings (and would need to be
> extended to numbers > 9).
If you're going to worry about non-ascii machines, you may as well
also worry about non-unix-inspired filesystems
(let ((filename (make-pathname :name "screen"
:type (princ-to-string integer))))
...
-dan
--
http://www.cliki.net/ - Link farm for free CL-on-Unix resources
Derek Hans <·········@gmx.net> writes:
The format function comes to mind:
http://www.lispworks.com/reference/HyperSpec/Body/f_format.htm
marc
> Hi
> I just started using lisp, and am quite amazed by its power. However,
> I stumbled onto a problem that just totally stumped me.
>
> I want to open a series of files, generically calle screen.1,
> screen.2, screen.3, etc... up to screen.90
>
> I figured that I could use a function that returns a string with the
> appropriate name. On each call, it changes the last two elements in
> the string vector. However,
> (setf (elt file-name 8) 3) gives an error because 3 isn't a character.
> I tried (setf (elt test 50) `#\,current-num), where current-num is the
> function that returns the incremented number, and (setf (elt test 50)
> `#\,(current-num))
> I've looked for a function that converts integers to character - sadly
> enough, integer-to-char only exists in emacs.
> write-string doesn't take integers either.
> I've looked through 2 tutorial, "Lisp, the Language" by Steele, and
> even the actual ANSI standard... I just don't know where else to
> search anylonger. Can someone help please?
>
> Derek
Derek Hans <·········@gmx.net> writes:
> I've looked for a function that converts integers to character - sadly
> enough, integer-to-char only exists in emacs.
It's called CODE-CHAR, though the mapping from code to char is
implementation dependent (probably not a problem in practice).
> write-string doesn't take integers either.
Thats why it's called WRITE-*STRING* after all... WRITE works with
integers, but doesn't return a string. PRINC-TO-STRING (or
WRITE-TO-STRING, or PRIN1-TO-STRING) would be another possibility, or
FORMAT.
Regards
Henrik
From: Eduardo Muñoz
Subject: Re: Integer to character conversion
Date:
Message-ID: <ubryt6i75.fsf@terra.es>
Derek Hans <·········@gmx.net> writes:
> Hi
> I just started using lisp, and am quite amazed by its power. However,
> I stumbled onto a problem that just totally stumped me.
>
> I want to open a series of files, generically calle screen.1,
> screen.2, screen.3, etc... up to screen.90
(loop for i from 1 to 90
for name = (format nil "screen.~D" i) do
;; Open file instead of print...
(print name))
--
Eduardo Mu�oz | (prog () 10 (print "Hello world!")
http://213.97.131.125/ | 20 (go 10))
Normally, I would just cons up a new string for this, rather than trying
to reuse the existing string. In that case it would be simply
(format nil "screen.~D" current-num)
Alternately if you really wanted to do digits yourself, you should use
something like:
(defvar *digits* "0123456789")
(setf (elt file-name 8) (elt *digits* current-num))
The reason this is a bit difficult is that the CL spec doesn't require
any particular character set, which limits the usefulness of certain
built-in functions like CODE-CHAR and CHAR-CODE.
--
Thomas A. Russ, USC/Information Sciences Institute ···@isi.edu