Symbol name to a string?

Symbol name to a string?

Anonymous
Not applicable
2,040 Views
7 Replies
Message 1 of 8

Symbol name to a string?

Anonymous
Not applicable
Hi,

Does anyone know if you can convert a symbol name to a string?

Eg.

(setq str "(A B C)") => "(A B C)"
(setq lst (read str)) => (A B C)
(function-x lst) => ("A" "B" "C")

Thanks in advance!

Andrew Wilford
visualdcl@mindspring.com

Visual DCL v1.0
http://www.mindspring.com/~cwilford/visualdcl.htm
------------Millennium special $89.95!-----------
0 Likes
2,041 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Vlisp: (vl-symbol-name symbol)

Using vanilla lisp you have to resort to writing to a temporary file and
then reading it back in.

__________________________________

Michael Puckett
programmer@cadvision.com
> Not < an Autodesk hall monitor.
Imagination makes all things possible.
__________________________________

"Andrew Wilford" wrote in message
news:38D2C652.84815964@mindspring.com...
Hi,

Does anyone know if you can convert a symbol name to a string?

Eg.

(setq str "(A B C)") => "(A B C)"
(setq lst (read str)) => (A B C)
(function-x lst) => ("A" "B" "C")

Thanks in advance!

Andrew Wilford
visualdcl@mindspring.com

Visual DCL v1.0
http://www.mindspring.com/~cwilford/visualdcl.htm
------------Millennium special $89.95!-----------
0 Likes
Message 3 of 8

Anonymous
Not applicable
Regarding: (function-x '(A B C)) => ("A" "B" "C")

Using ...

(mapcar 'vl-symbol-name '(a b c))

should return

("A" "B" "C")

although I never tested it.

__________________________________

Michael Puckett
programmer@cadvision.com
> Not < an Autodesk hall monitor.
Imagination makes all things possible.
__________________________________

"Andrew Wilford" wrote in message
news:38D2C652.84815964@mindspring.com...
Hi,

Does anyone know if you can convert a symbol name to a string?

Eg.

(setq str "(A B C)") => "(A B C)"
(setq lst (read str)) => (A B C)
(function-x lst) => ("A" "B" "C")

Thanks in advance!

Andrew Wilford
visualdcl@mindspring.com

Visual DCL v1.0
http://www.mindspring.com/~cwilford/visualdcl.htm
------------Millennium special $89.95!-----------
0 Likes
Message 4 of 8

Anonymous
Not applicable
Thanks Michael!

Although I am familiar with VL it is for a client who only has plain
lisp. I didn't think there was an easy way.

Thanks again.

Regards,

Andrew Wilford
visualdcl@mindspring.com

Visual DCL v1.0
http://www.mindspring.com/~cwilford/visualdcl.htm
------------Millennium special $89.95!-----------
0 Likes
Message 5 of 8

Anonymous
Not applicable
Don't give up man - it is easy.

(defun sym2str (sym / n f rtn)
(setq rtn "" n (strcat (getvar "tempprefix") "sym2str.tmp"))
(cond
( (setq f (open n "w"))
(prin1 sym f)
(setq f (close f))
(if (setq f (open n "r")) (setq rtn (read-line f) f (close f)))
)
)
rtn
)

eg (sym2str 'a) returns "A"

__________________________________

Michael Puckett
programmer@cadvision.com
> Not < an Autodesk hall monitor.
Imagination makes all things possible.
__________________________________

"Andrew Wilford" wrote in message
news:38D2D6BA.1CA7E8D1@mindspring.com...
Thanks Michael!

Although I am familiar with VL it is for a client who only has plain
lisp. I didn't think there was an easy way.

Thanks again.

Regards,

Andrew Wilford
visualdcl@mindspring.com

Visual DCL v1.0
http://www.mindspring.com/~cwilford/visualdcl.htm
------------Millennium special $89.95!-----------
0 Likes
Message 6 of 8

Anonymous
Not applicable
There's no need for opening a file (and leaving it cluttering up the
disk) in plain AutoLISP:

;;; Convert a symbol to its name as a string.

;;; Jon Fleming April 25, 1998 - based on code attributed
;;; to Bill O'Neal

;;; WARNING: DO NOT "compile" this function, in Visual
;;; LISP or Vital LISP, with "link" or "drop" options on!

(defun SymToStr (SymbolName)
;; If the argument is a symbol ...
(if (= (type SymbolName) 'SYM)
(progn
;; Execute the function we are going to create ...
(
;; Create a function on the fly
(list
;; The argument and local variable list. Note
;; that SymbolName will be evaluated, so the
;; symbol name that we're trying to convert
;; will be a local variable.
(list '/ SymbolName)
;; Set the local variable to 0 to make sure it
;; appears in the atoms-family (variables that
;; are "bound to NIL", explicitly or implicitly,
;; do not appear in the atoms-family)
(list set (quote SymbolName) 0)
;; In "native "AutoLISP", the symbol would
;; now be the first one in the atoms-family
;; list. Not so in Visual LISP. So we have
;; to look up the symbol [as a symbol] using
;; (member ...), compute its position in the
;; list, and then extract it from the string
;; version of the atoms-family list. This
;; also sets up the return value.
(list nth
(list -
(list length (list atoms-family 0))
(list length
(list member
(list (quote quote) SymbolName)
(list atoms-family 0)
) ;_ end list
) ;_ end list
) ;_ end list
(list atoms-family 1)
) ;_ end list
;; End of the dynamic function
) ;_ end list
)
) ;_ end progn
;; If the argument is not a symbol, return NIL
nil
) ;_ end if
) ;_ end defun

jrf
Member of the Autodesk Discussion Forum Moderator Program

In article <38D2D6BA.1CA7E8D1@mindspring.com>, Andrew Wilford wrote:
> Although I am familiar with VL it is for a client who only has plain
> lisp. I didn't think there was an easy way.
0 Likes
Message 7 of 8

Anonymous
Not applicable
Very clever. I remember this code being posted awhile ago. I had run it
and found it didn't work, tho that may have been a problem at my end.
Anyway, it works fine this time, and once all the comments are removed it's
quite tight - good one.

Speaking of using atoms-family ...

; michael puckett 2000/03/18

(defun sym2str (sym / str)
(if (eq 'sym (type sym))
(cond
( (eval sym)
(cdr
(assoc sym
(mapcar
'cons
(atoms-family 0)
(atoms-family 1)
)
)
)
)
( t
(set sym t)
(setq str (sym2str sym))
(set sym nil)
str
)
)
)
)

(sym2str 'abc) returns "ABC"

__________________________________

Michael Puckett
programmer@cadvision.com
> Not < an Autodesk hall monitor.
Imagination makes all things possible.
__________________________________

"Jon Fleming" wrote in message
news:VA.00000c22.0ab11d4b@fleming-group.com...
There's no need for opening a file (and leaving it cluttering up the
disk) in plain AutoLISP:

0 Likes
Message 8 of 8

Anonymous
Not applicable
Thanks guys!

Very clever indeed! I think he can manage that.

Regards,

Andrew Wilford
visualdcl@mindspring.com

Visual DCL v1.0
http://www.mindspring.com/~cwilford/visualdcl.htm
------------Millennium special $89.95!-----------
0 Likes