unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* looping over characters in a string in elisp?
@ 2002-12-25 23:53 Roy Smith
  2002-12-26  0:32 ` Jesper Harder
  2002-12-26 15:50 ` Kai Großjohann
  0 siblings, 2 replies; 5+ messages in thread
From: Roy Smith @ 2002-12-25 23:53 UTC (permalink / raw)


I'm probably being dense, but I can't figure out how to loop over each 
character in a string in elisp (I'm using emacs 20.5.1).  I know I can 
use mapchar to map a function to each character, but I'm looking for 
something that lets me do (essentially):

(while char in string
   (do stuff)
)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: looping over characters in a string in elisp?
  2002-12-25 23:53 looping over characters in a string in elisp? Roy Smith
@ 2002-12-26  0:32 ` Jesper Harder
  2002-12-26 15:50 ` Kai Großjohann
  1 sibling, 0 replies; 5+ messages in thread
From: Jesper Harder @ 2002-12-26  0:32 UTC (permalink / raw)


Roy Smith <roy@panix.com> writes:

> I can't figure out how to loop over each character in a string in
> elisp (I'm using emacs 20.5.1).  I know I can use mapchar to map a
> function to each character, but I'm looking for something that lets me
> do (essentially):
>
> (while char in string
>    (do stuff))

Here's two different ways:

(require 'cl)
(setq str "Test!")

(dolist (char (append str nil))
  (print char))

(dotimes (i (length str))
  (print (aref str i)))

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: looping over characters in a string in elisp?
  2002-12-25 23:53 looping over characters in a string in elisp? Roy Smith
  2002-12-26  0:32 ` Jesper Harder
@ 2002-12-26 15:50 ` Kai Großjohann
  2002-12-29  0:33   ` Ian Zimmerman
  1 sibling, 1 reply; 5+ messages in thread
From: Kai Großjohann @ 2002-12-26 15:50 UTC (permalink / raw)


Roy Smith <roy@panix.com> writes:

> I'm probably being dense, but I can't figure out how to loop over each 
> character in a string in elisp (I'm using emacs 20.5.1).  I know I can 
> use mapchar to map a function to each character, but I'm looking for 
> something that lets me do (essentially):
>
> (while char in string
>    (do stuff)
> )

Why does it have to be a while loop?  Why is a map-like look not
enough?

(mapcar (lambda (c) (char-to-string c)) "foobar")

This calls the lambda expression on each character and collects the
results in a list.  You can throw the result away if you don't like
it...

-- 
Do not be ashamed of ambibibentism.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: looping over characters in a string in elisp?
  2002-12-26 15:50 ` Kai Großjohann
@ 2002-12-29  0:33   ` Ian Zimmerman
  2002-12-29 11:37     ` Kai Großjohann
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Zimmerman @ 2002-12-29  0:33 UTC (permalink / raw)



>> (while char in string (do stuff) )

Kai> Why does it have to be a while loop?  Why is a map-like look not
Kai> enough?

Kai> (mapcar (lambda (c) (char-to-string c)) "foobar")

Kai> This calls the lambda expression on each character and collects
Kai> the results in a list.  You can throw the result away if you
Kai> don't like it...

I can remember the time(s) when I had forgotten enough about Emacs
Lisp that I wanted to ask this question.  I always checked myself in
time and found the answer, but I think this is (a little bit of)
evidence that TFM needs a simple improvement.

Just put "STRINGS ARE ARRAYS" in big fat font at the beginning of the
Strings section (which precedes the Sequences Arrays Vectors section).

-- 
Ian Zimmerman, Oakland, California, U.S.A. 
if (sizeof(signed) > sizeof(unsigned) + 4) { delete this; }
GPG: 433BA087  9C0F 194F 203A 63F7 B1B8  6E5A 8CA3 27DB 433B A087

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: looping over characters in a string in elisp?
  2002-12-29  0:33   ` Ian Zimmerman
@ 2002-12-29 11:37     ` Kai Großjohann
  0 siblings, 0 replies; 5+ messages in thread
From: Kai Großjohann @ 2002-12-29 11:37 UTC (permalink / raw)


Ian Zimmerman <itz@speakeasy.org> writes:

> Just put "STRINGS ARE ARRAYS" in big fat font at the beginning of the
> Strings section (which precedes the Sequences Arrays Vectors section).

8-)  Actually, the Strings and Characters node starts with

/----
|    A string in Emacs Lisp is an array that contains an ordered sequence
| of characters.
\----

Whee.  Not much that can be improved there, except for the big fat
font...
-- 
Ambibibentists unite!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-12-29 11:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-12-25 23:53 looping over characters in a string in elisp? Roy Smith
2002-12-26  0:32 ` Jesper Harder
2002-12-26 15:50 ` Kai Großjohann
2002-12-29  0:33   ` Ian Zimmerman
2002-12-29 11:37     ` Kai Großjohann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).