unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* how to convert a string to a symbol?
@ 2008-09-14  4:59 sunway
  2008-09-14  5:54 ` Drew Adams
  0 siblings, 1 reply; 13+ messages in thread
From: sunway @ 2008-09-14  4:59 UTC (permalink / raw
  To: help-gnu-emacs

(setq str "(> 2 1)")
I want something like:
(eval (string-to-symbol str))

is it possible in elisp?


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

* RE: how to convert a string to a symbol?
  2008-09-14  4:59 how to convert a string to a symbol? sunway
@ 2008-09-14  5:54 ` Drew Adams
  2008-09-14  6:10   ` Drew Adams
       [not found]   ` <mailman.19202.1221372658.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 13+ messages in thread
From: Drew Adams @ 2008-09-14  5:54 UTC (permalink / raw
  To: 'sunway', help-gnu-emacs

> (setq str "(> 2 1)")
> I want something like: (eval (string-to-symbol str))

(intern str)





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

* RE: how to convert a string to a symbol?
  2008-09-14  5:54 ` Drew Adams
@ 2008-09-14  6:10   ` Drew Adams
       [not found]   ` <mailman.19202.1221372658.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 13+ messages in thread
From: Drew Adams @ 2008-09-14  6:10 UTC (permalink / raw
  To: 'sunway', help-gnu-emacs

> > (setq str "(> 2 1)")
> > I want something like: (eval (string-to-symbol str))
> 
> (intern str)

And if you really want the value of the symbol (per your use of `eval' above):

(symbol-value (intern str))

But you might want to give it a value first ;-) -

(set (intern "(> 2 1)") 42)

(symbol-value '\(>\ 2\ 1\)) ; The answer is 42.

However, as always with questions of this type, one wonders what you are
_really_ trying to do (not to mention why)...





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

* Re: how to convert a string to a symbol?
       [not found]   ` <mailman.19202.1221372658.18990.help-gnu-emacs@gnu.org>
@ 2008-09-14  7:10     ` sunway
  2008-09-14  7:27       ` Pascal J. Bourguignon
  2008-09-14  8:06       ` Tim X
  2008-09-14  7:25     ` Pascal J. Bourguignon
  2008-09-14 10:19     ` Richard G Riley
  2 siblings, 2 replies; 13+ messages in thread
From: sunway @ 2008-09-14  7:10 UTC (permalink / raw
  To: help-gnu-emacs

I don't  want to take "(> 2 1)" as a symbol name, instead I want  "(>
2 1)" be evaled to 't'

On Sep 14, 2:10 pm, "Drew Adams" <drew.ad...@oracle.com> wrote:
> > > (setq str "(> 2 1)")
> > > I want something like: (eval (string-to-symbol str))
>
> > (intern str)
>
> And if you really want the value of the symbol (per your use of `eval' above):
>
> (symbol-value (intern str))
>
> But you might want to give it a value first ;-) -
>
> (set (intern "(> 2 1)") 42)
>
> (symbol-value '\(>\ 2\ 1\)) ; The answer is 42.
>
> However, as always with questions of this type, one wonders what you are
> _really_ trying to do (not to mention why)...



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

* Re: how to convert a string to a symbol?
       [not found]   ` <mailman.19202.1221372658.18990.help-gnu-emacs@gnu.org>
  2008-09-14  7:10     ` sunway
@ 2008-09-14  7:25     ` Pascal J. Bourguignon
  2008-09-14 10:19     ` Richard G Riley
  2 siblings, 0 replies; 13+ messages in thread
From: Pascal J. Bourguignon @ 2008-09-14  7:25 UTC (permalink / raw
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

>> > (setq str "(> 2 1)")
>> > I want something like: (eval (string-to-symbol str))
>> 
>> (intern str)
>
> And if you really want the value of the symbol (per your use of `eval' above):
>
> (symbol-value (intern str))
>
> But you might want to give it a value first ;-) -
>
> (set (intern "(> 2 1)") 42)
>
> (symbol-value '\(>\ 2\ 1\)) ; The answer is 42.
>
> However, as always with questions of this type, one wonders what you are
> _really_ trying to do (not to mention why)...

sunways, what Drew tries to hint at is that you don't want
string-to-symbol.

(By the way, there's another way to get a symbol from a string, it's
make-symbol, but it makes an uninterned symbol, which is even less
what you want).

So as you may have realized by now, what you want is to get the
symbolic expression, or s-exp (> 2 1) from "(> 2 1)", and this can be
done by "reading" the string, so, logically, with read-from-string.
(car (read-from-string "(> 2 1)")) -> (> 2 1)


Now the question would be where did you get this string from in the
first place?  You probably could have used read, or specified you
wanted a s-exp instead of a string at the source.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
You never feed me.
Perhaps I'll sleep on your face.
That will sure show you.


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

* Re: how to convert a string to a symbol?
  2008-09-14  7:10     ` sunway
@ 2008-09-14  7:27       ` Pascal J. Bourguignon
  2008-09-14  8:06       ` Tim X
  1 sibling, 0 replies; 13+ messages in thread
From: Pascal J. Bourguignon @ 2008-09-14  7:27 UTC (permalink / raw
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> I don't  want to take "(> 2 1)" as a symbol name, instead I want  "(>
> 2 1)" be evaled to 't'

Oh, that's easy:

(defun eval-to-t (expr)
   't)

(eval-to-t "(> 2 1)") --> t


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
You never feed me.
Perhaps I'll sleep on your face.
That will sure show you.


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

* Re: how to convert a string to a symbol?
  2008-09-14  7:10     ` sunway
  2008-09-14  7:27       ` Pascal J. Bourguignon
@ 2008-09-14  8:06       ` Tim X
  2008-09-14  8:34         ` sunway
  1 sibling, 1 reply; 13+ messages in thread
From: Tim X @ 2008-09-14  8:06 UTC (permalink / raw
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> I don't  want to take "(> 2 1)" as a symbol name, instead I want  "(>
> 2 1)" be evaled to 't'
>

Your question, or at least the way you have phrased it is misleading as
you are (possibly) using terminology incorrectly. try asking your
question with some background on what it is your trying to do. 

For example, (this is just a 'guess' from reading between the lines of
what yu have posted and from similar questions I've seen in the past)

"I'm developing a small package where I want the user to be able to
enter some lisp expression via the mini-buffer and have elisp evaluate
it and return the result. However, I'm having problems working out how
to get the data entered by the user evaluated as if it was a lisp
expression. I think what I need to do is somehow turn the string entered
by the user into an s-expression and have emacs evaluate it and return
the result. Where do I start?"

If the above is close to wht you want, chaeck out the documentation to
interactive and the x or X args in particular. If its not, let us know
more!

Tim



-- 
tcross (at) rapttech dot com dot au


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

* Re: how to convert a string to a symbol?
  2008-09-14  8:06       ` Tim X
@ 2008-09-14  8:34         ` sunway
  2008-09-14  9:25           ` Joost Diepenmaat
  2008-09-14 13:47           ` Pascal J. Bourguignon
  0 siblings, 2 replies; 13+ messages in thread
From: sunway @ 2008-09-14  8:34 UTC (permalink / raw
  To: help-gnu-emacs

What you say is close to what I want, I do want to turn a string into
a sexp,but the string could from the minibuffer or somewhere else.
for example, I want to write a function , it takes a string as
argument, the function could turn the string to a sexp, eval it, and
return the result.


On Sep 14, 4:06 pm, Tim X <t...@nospam.dev.null> wrote:
> sunway <sunwayfore...@gmail.com> writes:
> > I don't  want to take "(> 2 1)" as a symbol name, instead I want  "(>
> > 2 1)" be evaled to 't'
>
> Your question, or at least the way you have phrased it is misleading as
> you are (possibly) using terminology incorrectly. try asking your
> question with some background on what it is your trying to do.
>
> For example, (this is just a 'guess' from reading between the lines of
> what yu have posted and from similar questions I've seen in the past)
>
> "I'm developing a small package where I want the user to be able to
> enter some lisp expression via the mini-buffer and have elisp evaluate
> it and return the result. However, I'm having problems working out how
> to get the data entered by the user evaluated as if it was a lisp
> expression. I think what I need to do is somehow turn the string entered
> by the user into an s-expression and have emacs evaluate it and return
> the result. Where do I start?"
>
> If the above is close to wht you want, chaeck out the documentation to
> interactive and the x or X args in particular. If its not, let us know
> more!
>
> Tim
>
> --
> tcross (at) rapttech dot com dot au



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

* Re: how to convert a string to a symbol?
  2008-09-14  8:34         ` sunway
@ 2008-09-14  9:25           ` Joost Diepenmaat
  2008-09-14 13:47           ` Pascal J. Bourguignon
  1 sibling, 0 replies; 13+ messages in thread
From: Joost Diepenmaat @ 2008-09-14  9:25 UTC (permalink / raw
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> What you say is close to what I want, I do want to turn a string into
> a sexp,but the string could from the minibuffer or somewhere else.
> for example, I want to write a function , it takes a string as
> argument, the function could turn the string to a sexp, eval it, and
> return the result.

You want to take a look at (read-from-string) or possibly some of the
other read* functions.

also: please don't top post. it really makes your posts harder to read.

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

* Re: how to convert a string to a symbol?
       [not found]   ` <mailman.19202.1221372658.18990.help-gnu-emacs@gnu.org>
  2008-09-14  7:10     ` sunway
  2008-09-14  7:25     ` Pascal J. Bourguignon
@ 2008-09-14 10:19     ` Richard G Riley
  2008-09-14 13:43       ` Pascal J. Bourguignon
  2 siblings, 1 reply; 13+ messages in thread
From: Richard G Riley @ 2008-09-14 10:19 UTC (permalink / raw
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

>> > (setq str "(> 2 1)")
>> > I want something like: (eval (string-to-symbol str))
>> 
>> (intern str)
>
> And if you really want the value of the symbol (per your use of `eval' above):
>
> (symbol-value (intern str))
>
> But you might want to give it a value first ;-) -
>
> (set (intern "(> 2 1)") 42)
>
> (symbol-value '\(>\ 2\ 1\)) ; The answer is 42.
>
> However, as always with questions of this type, one wonders what you are
> _really_ trying to do (not to mention why)...

It seems from the mention of eval quite clear (not being an elisp
programmer myself). He wants the result of the expression held in the
string str.

e.g

(setq str "(> 2 1)")

When str is "Eval"ed however you guys tell him to do it then this
expression will yield the value of the expression (> 2 1) which is, in
this case, 't.



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

* Re: how to convert a string to a symbol?
  2008-09-14 10:19     ` Richard G Riley
@ 2008-09-14 13:43       ` Pascal J. Bourguignon
  2008-09-15  9:34         ` David Kastrup
  0 siblings, 1 reply; 13+ messages in thread
From: Pascal J. Bourguignon @ 2008-09-14 13:43 UTC (permalink / raw
  To: help-gnu-emacs

Richard G Riley <rileyrgdev@gmail.com> writes:
> It seems from the mention of eval quite clear (not being an elisp
> programmer myself). He wants the result of the expression held in the
> string str.

We know what he wants.  But we're trying to make him express his needs
better.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

CONSUMER NOTICE: Because of the "uncertainty principle," it is
impossible for the consumer to simultaneously know both the precise
location and velocity of this product.


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

* Re: how to convert a string to a symbol?
  2008-09-14  8:34         ` sunway
  2008-09-14  9:25           ` Joost Diepenmaat
@ 2008-09-14 13:47           ` Pascal J. Bourguignon
  1 sibling, 0 replies; 13+ messages in thread
From: Pascal J. Bourguignon @ 2008-09-14 13:47 UTC (permalink / raw
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> What you say is close to what I want, I do want to turn a string into
> a sexp,but the string could from the minibuffer or somewhere else.
> for example, I want to write a function , it takes a string as
> argument, the function could turn the string to a sexp, eval it, and
> return the result.

From the minibuffer, you can read the s-expression directly.

Read the documentation of read-from-minibuffer!

(eval (read-from-minibuffer "Expression: " nil nil t))


Since you mention somewhere else, I'll mention also read:

(with-temp-buffer 
  (insert "(+ 1 2)\n(* 3 4)\n")
  (goto-char (point-min))
  (list (eval (read (current-buffer)))
        (eval (read (current-buffer)))))

Read the documentation of read!

If you don't know it already: 
C-h f read RET
C-h f read-from-minibuffer RET

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

CONSUMER NOTICE: Because of the "uncertainty principle," it is
impossible for the consumer to simultaneously know both the precise
location and velocity of this product.


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

* Re: how to convert a string to a symbol?
  2008-09-14 13:43       ` Pascal J. Bourguignon
@ 2008-09-15  9:34         ` David Kastrup
  0 siblings, 0 replies; 13+ messages in thread
From: David Kastrup @ 2008-09-15  9:34 UTC (permalink / raw
  To: help-gnu-emacs

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> Richard G Riley <rileyrgdev@gmail.com> writes:
>> It seems from the mention of eval quite clear (not being an elisp
>> programmer myself). He wants the result of the expression held in the
>> string str.
>
> We know what he wants.  But we're trying to make him express his needs
> better.

I wasn't aware it is national "make fun of newbies" week again.

Anyway,

(eval (read "(< 1 2)"))  ->  t

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


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

end of thread, other threads:[~2008-09-15  9:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-14  4:59 how to convert a string to a symbol? sunway
2008-09-14  5:54 ` Drew Adams
2008-09-14  6:10   ` Drew Adams
     [not found]   ` <mailman.19202.1221372658.18990.help-gnu-emacs@gnu.org>
2008-09-14  7:10     ` sunway
2008-09-14  7:27       ` Pascal J. Bourguignon
2008-09-14  8:06       ` Tim X
2008-09-14  8:34         ` sunway
2008-09-14  9:25           ` Joost Diepenmaat
2008-09-14 13:47           ` Pascal J. Bourguignon
2008-09-14  7:25     ` Pascal J. Bourguignon
2008-09-14 10:19     ` Richard G Riley
2008-09-14 13:43       ` Pascal J. Bourguignon
2008-09-15  9:34         ` David Kastrup

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).