Page 1 of 1

How do I create a variables name from scratch and run it

Posted: Wed May 07, 2014 3:39 am
by joeish80829
Normally I can do (defparameter N 1) and run "N" at the repl and it would output 1.
Well I have variables from another package I need to setf to the variable "A"(for example)...The variables can be anything but they all start with the prefix "cv::" w/o quotes eg cv::b, cv::test,
cv::x ...Lets say the variable cv::b equals 500,

so

Code: Select all

REPL> cv::b
500
I need a function with one parameter like this:

Code: Select all

(defun foo (x)
"other stuff here"
)
so if I run (foo b) it will set the variable cv::b that is already created and equals 500 to a new variable "A" in side the function. so after I runn (foo b). I can evaluate "A" at the REPL and the output would be"

Code: Select all

REPL> A

500

Can someone help me do this?

Re: How do I create a variables name from scratch and run i

Posted: Wed May 07, 2014 9:51 am
by porky11
I'm not sure, what you want to do.
If you define a function, b is evaluated, so you should use a macro or quote the b.

if you want to set the value of a to the value of cv::b you can write this function

Code: Select all

(defun foo (x)
  "do something that makes variable a global, if it is not"
  (setq a (symbol-value (read-from-string (format nil "cv::~a" x)))))
You have to call

Code: Select all

(foo 'b)

Re: How do I create a variables name from scratch and run i

Posted: Thu May 08, 2014 1:19 am
by joeish80829
Thank you very much that worked perfect

Re: How do I create a variables name from scratch and run i

Posted: Sat May 10, 2014 2:34 pm
by Goheeca
Or more programatically or how to say. Not involving the read function.

Code: Select all

(defun foo (x)
  (setf a (symbol-value (intern (symbol-name x) (find-package :cv)))))
Due to this exercise I found out that symbol-package is not setfable hence I used intern.

Re: How do I create a variables name from scratch and run i

Posted: Sun May 11, 2014 1:12 am
by joeish80829
Thanks...I ended up just creating a different .asd file and and that solved the issue...That's why I was trying to get this info to convert a variable from 1 package name to another