Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I use a var as a string?

11 views
Skip to first unread message

Tom Browder

unread,
Jul 28, 2016, 12:42:39 PM7/28/16
to help-gn...@gnu.org
I want to bind a string to a variable and then use the variable name as the
string when a string is expected. I have tried variations of this:

(defvar my-str (concat "my " "string"))
(print my-str)

and get an error about void symbol my-str.

I have tried putting an apostrophe in front of various places but haven't
yet found a working solution. So how can I define a variable whose value
is a string and then access the actual string value by using the variable
name?

Thanks.

Best regards,

-Tom

Emanuel Berg

unread,
Jul 28, 2016, 12:46:58 PM7/28/16
to
Tom Browder wrote:

> So how can I define a variable whose value is
> a string and then access the actual string
> value by using the variable name?

For example like this:

(defun print-string (s)
(insert "The string is: " s) )

(setq sail-ho-string "True sailing is dead!")

(print-string sail-ho-string)

--
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
- so far: 58 Blogomatic articles -

Drew Adams

unread,
Jul 28, 2016, 12:47:58 PM7/28/16
to Tom Browder, help-gn...@gnu.org
> (defvar my-str (concat "my " "string"))
> (print my-str)
>
> and get an error about void symbol my-str.

It works for me. Did you evaluate the `defvar', before trying to
evaluate the `print' - it sounds like you did not.

Tom Browder

unread,
Jul 28, 2016, 1:11:12 PM7/28/16
to Drew Adams, help-gn...@gnu.org
Duh, no I didn't! Then it does work! (I should be evaling the buffer for
the experimenting I'm doing.)

Thanks, Drew.

-Tom

Emanuel Berg

unread,
Jul 28, 2016, 10:39:04 PM7/28/16
to
Tom Browder wrote:

> Duh, no I didn't! Then it does work! (I
> should be evaling the buffer for the
> experimenting I'm doing.)

You can, with `load-file' and then RET (no
input data implies the current buffer), but it
is more to the point to use either
`eval-last-sexp' (C-x C-e) or `eval-defun'
which I have C-m.

Tom Browder

unread,
Jul 29, 2016, 10:50:47 AM7/29/16
to help-gn...@gnu.org
On Thursday, July 28, 2016, Tom Browder <tom.b...@gmail.com> wrote:

> I want to bind a string to a variable and then use the variable name as
> the string when a string is expected. I have tried variations of this:
>
> (defvar my-str (concat "my " "string"))
>

My next experiment will be to use "(defconst ...)" which I'm hoping will
solve the problem.

-Tom

Drew Adams

unread,
Jul 29, 2016, 11:00:06 AM7/29/16
to Tom Browder, help-gn...@gnu.org
> > I want to bind a string to a variable and then use the variable name as
> > the string when a string is expected. I have tried variations of this:
> > (defvar my-str (concat "my " "string"))
>
> My next experiment will be to use "(defconst ...)" which I'm hoping will
> solve the problem.

What problem? It's not clear (to me) what you are trying to do.

Tom Browder

unread,
Jul 29, 2016, 11:06:23 AM7/29/16
to Drew Adams, help-gn...@gnu.org
On Friday, July 29, 2016, Drew Adams <drew....@oracle.com> wrote:

> > > I want to bind a string to a variable and then use the variable name as
> > > the string when a string is expected. I have tried variations of this:
> > > (defvar my-str (concat "my " "string"))

...

> > My next experiment will be to use "(defconst ...)" which I'm hoping will
> > solve the problem.
>
> What problem? It's not clear (to me) what you are trying to do.
>

In the beginning of this thread I mentioned I was getting an error when
trying to use the var in another definition.

-Tom

John Mastro

unread,
Jul 29, 2016, 1:20:51 PM7/29/16
to help-gn...@gnu.org, Tom Browder
Tom Browder <tom.b...@gmail.com> wrote:
> Here is the best I can do for the moment to show my actual problem.
> The code is from an attempt at modifying the following code chunk
> which is part of the file "perl6-imenu.el" (branch "my-branch") found
> at my github account at:
>
> https://github.com/tbrowder/perl6-mode
>
> The working code chunk is at:
>
> https://gist.github.com/tbrowder/effb3ed0540591506015846b0511a045
>
> and the chunk I'm trying to replace it with is here:
>
> https://gist.github.com/tbrowder/7959e77fcf5aee4be3edb342503cc282
>
> The substitution of the explicit regex with the string var doesn't
> work. It may be because of some problem with the scope of the
> variables, and that is WAY beyond my elisp understanding at the
> moment.

I haven't followed this discussion in detail, but try this[1].

You used a quoted list in your definition of
perl6-imenu-generic-expression, meaning the list isn't evaluated,
meaning it contained the symbol `perl6-vars' rather than the value bound
to that symbol (the regular expression you constructed). Instead, you
probably want to use backquote.

This is all a bit difficult to explain succinctly in an email, but this
is a common point of confusion so there's plenty of material on "quote"
and "backquote" (or sometimes "quasiquote") in Lisp out there. In
Emacs's Elisp manual, check out the node "(elisp) Backquote".

[1] https://gist.github.com/johnmastro/53535e8cbddf7c669788bc2a9105f70e

Hope that helps

John

John Mastro

unread,
Jul 29, 2016, 1:24:51 PM7/29/16
to help-gn...@gnu.org, Tom Browder
John Mastro <john.b...@gmail.com> wrote:
> I haven't followed this discussion in detail, but try this[1].

One thing I can say with confidence is that the issue is not related to
`defvar' vs. `defconst' :)

John

Tom Browder

unread,
Jul 29, 2016, 1:56:51 PM7/29/16
to John Mastro, help-gn...@gnu.org
On Friday, July 29, 2016, John Mastro <john.b...@gmail.com> wrote:

> John Mastro <john.b...@gmail.com <javascript:;>> wrote:
> > I haven't followed this discussion in detail, but try this[1].


That looks like an excellent lead, John (and not easy to find for a noob
without help from old heads). I read the entries in the elisp manual and
am anxious to try it.

One thing I can say with confidence is that the issue is not related to
> `defvar' vs. `defconst' :)


Well I can confirm that now, too!

Thanks so much--I'll report back after I get back to my PC.

Best regards,

-Tom

Tom Browder

unread,
Jul 29, 2016, 2:40:44 PM7/29/16
to John Mastro, help-gn...@gnu.org
On Fri, Jul 29, 2016 at 12:56 PM, Tom Browder <tom.b...@gmail.com> wrote:
> On Friday, July 29, 2016, John Mastro <john.b...@gmail.com> wrote:
>> John Mastro <john.b...@gmail.com> wrote:
>> > I haven't followed this discussion in detail, but try this[1].
...
> Thanks so much--I'll report back after I get back to my PC.

Okay, it worked!!!! I changed this def:

(defvar perl6-imenu-generic-expression
'( ; <= note quote (') instead of backquote (`)
; note explicit regex string below as second list item:
("Variables"
"^\\s-*\\(?:my\\|our\\)\\s-+\\(\\(?:\\$\\|@\\|%\\)\\(?:[_[:alnum:]]+\\)\\)"
1)
))

to this:

(defvar perl6-imenu-generic-expression
`( ; <= note backquote (`) instead of quote (')
; note regex string variable below (perl6-vars) as
second list item:
("Variables" ,perl6-vars 1) ; <= note comma immediately before the
regex var name (perl6-vars)
))

Thank you John! You da man!!

Best regards, and happy elisping,

-Tom

0 new messages