KennyBartMan
KennyBartMan

Reputation: 960

How can I dereference an emacs lisp variable?

  (defun save-interface-file ()
      (interactive)
      (let* ((xml (buffer-string))
             (root (with-temp-buffer (insert xml) (xml-parse-region (point-min) (point-max))))
             (request (car root))
             (nodename (xml-node-name request))
             (CustomerAgencyDataLoad "CALI")
             )
             (message "%s.CRM66898.VZCRMCAR%s.%s" (number-to-string (random 100000000)) (derefernce nodename) (number-to-string (random 100000)) )

        )
      )

I have the above function defined in my emacs.init file I open a buffer containing xml and type

M-x save-interface-file

What happens is the variable node name will contain the value "CustomerAgencyDataLoad". In the message I want to dereference this from nodename and print "CALI". If I print nodename I get "CustomerAgencyDataLoad". I know this sort of dereferencing works in perl using $$var however how do I achieve this in emacs LISP?

Upvotes: 0

Views: 495

Answers (1)

legoscia
legoscia

Reputation: 41568

While you can deference a variable using (symbol-value (intern "my-string")) (except if your code is compiled using lexical binding), in this case I'd suggest using an alist (short for "association list") instead:

(let ((shorter-names '((CustomerAgencyDataLoad . "CALI")
                       (SomethingElse . "FOO")))
      (node-name 'CustomerAgencyDataLoad))
  (cdr (assq node-name shorter-names)))

assq searches through shorter-names for an entry that matches the value of node-name. With cdr, we get the second half of the entry, which is "CALI" in this case.

Upvotes: 2

Related Questions