From: Jingxiang He
Subject: Two questions on string operation
Date: 
Message-ID: <1992Apr23.104752.10591@news.Hawaii.Edu>
Dear Netters,

I have two questions which might be very simple. But I tried several
ways and could not solve it. Can anyone help me out ?

1) How can I evaluate one variable in a string. In list we can use
   '( ,x) do it.
  e.g (setq a 'aaa)
      There is a string "abc def a"
      I want to get the result "abc def aaa"

2) How can I simply convert a list to a string
   e.g The list is ("aaa:bbb" ccc "ddd")
       I want the result string as "aaa:bbb ccc ddd"

I appreciate your help. Thanks.

J. He

From: Erik Gilbert
Subject: Re: Two questions on string operation
Date: 
Message-ID: <EJG.92Apr23104103@karoshi.lucid.com>
····@uhunix.uhcc.Hawaii.Edu (Jingxiang He) writes:
>2) How can I simply convert a list to a string
>   e.g The list is ("aaa:bbb" ccc "ddd")
>       I want the result string as "aaa:bbb ccc ddd"

(Uh, Quux, are you out there?)  My suggestions for minimalist answers are:

> (format nil "~{~a~^ ~}" '("aaa:bbb" ccc "ddd"))
"aaa:bbb CCC ddd"

...or, if you really *want* lower case...

> (format nil "~{~(~a~)~^ ~}" '("aaa:bbb" ccc "ddd"))
"aaa:bbb ccc ddd"

- Erik   ···@lucid.com    #include all applicable disclaimers
From: Marty Hall
Subject: Re: Two questions on string operation
Date: 
Message-ID: <1992Apr23.125842.15776@aplcen.apl.jhu.edu>
····@uhunix.uhcc.Hawaii.Edu (Jingxiang He) writes:
[...]
>  e.g (setq a 'aaa)
>      There is a string "abc def a"
>      I want to get the result "abc def aaa"

Take a look at the "read-from-string" function, which can take :start and
:end qualifiers. You probably want to read "a", use "symbol-value" to get
its value, then paste a string back together. Just be careful with 
read-from-string: it takes two optional args *before* the :start and :key.

If you don't know ahead of time which entries, if interpreted by read, would
be variables with values, then you would need to loop through with 
read-from-string (it returns the end position of the current read as a
multiple value), checking each result with "boundp".

Alternatively, why use strings at all here? If you are going to be using
symbols, why not use them directly? Ie just have a list of symbols, some of
which are bound. You can always make a string or a pretty printout when 
done using "format"...

>2) How can I simply convert a list to a string
>   e.g The list is ("aaa:bbb" ccc "ddd")
>       I want the result string as "aaa:bbb ccc ddd"

I think "concatenate" is the function you need:

(apply #'concatenate 'string (mapcar #'string '("aaa:bbb" ccc "ddd")))
---> "aaa:bbbcccddd"

You need a small amount of extra hacking if you want to add spaces in between,
and mapcar "princ-to-string" instead of "string" if your list contained 
numbers.

					- Marty
------------------------------------------------------
····@aplcen.apl.jhu.edu, ···········@jhunix.bitnet, ..uunet!aplcen!hall
Artificial Intelligence Lab, AAI Corp, PO Box 126, Hunt Valley, MD 21030

(setf (need-p 'disclaimer) NIL)
From: [Invalid-From-Line]
Subject: Re: Two questions on string operation
Date: 
Message-ID: <1992Apr28.051324.2495@Silence.pi.nctu.edu.tw>
From: [Invalid-From-Line]
Subject: Re: Two questions on string operation
Date: 
Message-ID: <1992Apr28.051455.3257@Silence.pi.nctu.edu.tw>
This article was probably generated by a buggy news reader.
From: Barry Margolin
Subject: Re: Two questions on string operation
Date: 
Message-ID: <kvddbdINNf8o@early-bird.think.com>
In article <······················@news.Hawaii.Edu> ····@uhunix.uhcc.Hawaii.Edu (Jingxiang He) writes:
>1) How can I evaluate one variable in a string. In list we can use
>   '( ,x) do it.

I assume you meant `(,x).

>  e.g (setq a 'aaa)
>      There is a string "abc def a"
>      I want to get the result "abc def aaa"

You don't say how you mark the part of the string that should be evaluated
(like comma in backquote notation).  I'll assume you meant something like
"abc def ,a"; the function below (which I haven't tested) takes the
indicator character as a parameter.

(defun eval-in-string (string comma)
  (loop with string-length (length string)
	with result = "" and read-value and end-of-read
	for start = 0 then end-of-read
	for comma-location = (position comma string :start start)
	do (setq result (concatenate 'string result (subseq string start comma-location)))
	while comma-location do
	   (multiple-value-setq (read-value end-of-read)
				(read-from-string string t nil :start (1+ comma-location)))
	   (setq result (concatenate 'string result
				      (prin1-to-string (eval read-value))
				      " "))
	finally return result))

Note that this doesn't have access to lexical variables as backquote does,
because it uses EVAL at run time.  For lexical access, you'd have to use a
different quoting character to mark the string and write a read macro that
expands these string into code that constructs the resulting string (just
as backquote does for lists).  You don't say why you need this, so I don't
know whether this is a significant problem for you.

>2) How can I simply convert a list to a string
>   e.g The list is ("aaa:bbb" ccc "ddd")
>       I want the result string as "aaa:bbb ccc ddd"

(defun list-to-string (list)
  (let ((result "") (first-time t))
    (dolist (item list result)
      (setq result (concatenate 'string result (if first-time "" " ")
				(princ-to-string item)))
      (when first-time
	(setq first-time nil)))))

This will normally result in "aaa:bbb CCC ddd", by the way, since symbol
names are normally upcased by READ.  If you wanted a lowercase symbol you
should have used ("aaa:bbb" |ccc| "ddd").
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Thomas A. Russ
Subject: Re: Two questions on string operation
Date: 
Message-ID: <21271@venera.isi.edu>
In article ... ······@think.com (Barry Margolin) writes:

   In article ... ····@uhunix.uhcc.Hawaii.Edu (Jingxiang He) writes:

	[First problem & solution deleted]
   >2) How can I simply convert a list to a string
   >   e.g The list is ("aaa:bbb" ccc "ddd")
   >       I want the result string as "aaa:bbb ccc ddd"

   (defun list-to-string (list)
     (let ((result "") (first-time t))
       (dolist (item list result)
	 (setq result (concatenate 'string result (if first-time "" " ")
				   (princ-to-string item)))
	 (when first-time
	   (setq first-time nil)))))

   This will normally result in "aaa:bbb CCC ddd", by the way, since symbol
   names are normally upcased by READ.  If you wanted a lowercase symbol you
   should have used ("aaa:bbb" |ccc| "ddd").
   -- 

How about the much simpler:

   (defun list-to-string (list)
     (format nil "~{~A~^ ~}" list))

This also allows one to specify the case of the result using other
format operators around the "~A".


-- Tom Russ