How can I get a string from a variable name?

James Maeding

Usenet Archive
Joined
Jul 7, 2003
Messages
331
Reaction score
0
Lets say I have a variable called Var1 and I want to obtain a string from that variable name. I know how to make a variable name from a string with (read "Var1"), but how do i go the other way?

I am trying to read string values from a text file and make a list of them. If I use (read (strcat "(" (read-line file) ")") it returns a nice list but the strings are considered variables in the list. Odd since numbers are detected... So I want to convert those variable names returned to strings.

I have other ways of doing this but a friend asked me and I didn't know the answer (not too suprising...) Any help appreciated. James Maeding Civil Engineer/Programmer
 
like Jason, I'm not clear on the question

do you mean (vl-symbol-name 'Var1)
returns
"Var1" ???

or
(setq var1 "Test")
(read (strcat "(" (eval var1) ")"))
returns
(TEST)
_$

James Maeding said:
Lets say I have a variable called Var1 and I want to obtain a string from that variable name. I know how to make a variable name from a string with (read "Var1"), but
how do i go the other way?
 
vl-princ-to-string ?

Not sure I completely understand the question

James Maeding said:
Lets say I have a variable called Var1 and I want to obtain a string from that variable name. I know how to make a variable name from a string with (read "Var1"), but how do i go the other way?

I am trying to read string values from a text file and make a list of them. If I use (read (strcat "(" (read-line file) ")") it returns a nice list
but the strings are considered variables in the
list.
Odd since numbers are detected...
So I want to convert those variable names returned to strings.

I have other ways of doing this but a friend asked me and I didn't know
the answer (not too suprising...)
 
Why not something like this, using John Uhden's "str2list" function. This assumes the use of a space as the delimiter in the text file:

(while (setq txtline (read-line file))
(setq txtlist (cons (str2list txtline " ") txtlist))
)

(defun Str2List (str pat / i j n lst)
(cond
((/= (type str)(type pat) 'STR))
((= str pat)'(""))
(T
(setq i 0 n (strlen pat))
(while (setq j (vl-string-search pat str i))
(setq lst (cons (substr str (1+ i)(- j i)) lst)
i (+ j n)
)
)
(reverse (cons (substr str (1+ i)) lst))
)
)
)

HTH,
Jeff

James Maeding said:
Lets say I have a variable called Var1 and I want to obtain a string from that variable name. I know how to make a variable name from a string with (read "Var1"), but how do i go the other way?

I am trying to read string values from a text file and make a list of them. If I use (read (strcat "(" (read-line file) ")") it returns a nice list
but the strings are considered variables in the
list.
Odd since numbers are detected...
So I want to convert those variable names returned to strings.

I have other ways of doing this but a friend asked me and I didn't know
the answer (not too suprising...)
 
When you do a (read (strcat "(" (read-line file) ")"), if you mean that it returns something like:
(("abc")( "123")), and you want to set a variable - Var1 to the 1st value..
Do:
(setq my_list (read-line file)); read a line
(setq Var1 (nth 0 my_list)); gets the "abc"
(setq Var2 (nth 1 my_list)); gets the "123"
Or, to make a 'long' list of the file:
Do:
(open file "r")
(while
(setq new_list (read-line file))
(setq long_list (cons new_list long_list)); concat to long list );end while (close file) !long_list should show all data to return a specific item, you need to 'point' to that item, beginning with 0, and incrementing.
Example:
(setq ctr 0)
(repeat 10
(setq new_str (nth ctr long_list)); get item
(prompt (strcat "\nFound: " new_str)); echo it to screen (setq ctr (+ ctr 1)); and increment ); end repeat

Bob
 
James:

T'would be nice to post a few lines from the file. Sounds to me as thought file should have been written with strings in quotes so you can programatically discern the difference between string and numbers.

I'll guess that a line of data may look like (Var1 (1 2 3)(A B C)) which could have happened by using... (princ (cons "Var1" var1) fp) where Var1 = '((1 2 3)("A" "B" "C")) Had the file been written using prin1 instead, life would be easy. But alas, now there is no way except by presumption to distinguish the integer 1 from the character "1" Wait second... this may help a little:

(defun FixList (items)
(cond
((vl-consp items)
(setq items (cons (FixList (car items))(FixList (cdr items)))) ) ((listp items) (foreach item items (setq items (subst (FixList item) item items)) ) ) ((vl-symbolp Items) (setq Items (vl-princ-to-string Items)) ) ) items )

Now this still isn't smart enough to interpret strings like 192.168.0.1 or that (Mr. Rube Goldberg) is actually supposed to be one name with only first caps.

James Maeding said:
Lets say I have a variable called Var1 and I want to obtain a string from that variable name. I know how to make a variable name from a string with (read "Var1"), but how do i go the other way?

I am trying to read string values from a text file and make a list of them. If I use (read (strcat "(" (read-line file) ")") it returns a nice list but
the strings are considered variables in the
 
Ah, Mark hit it on the head.
The vl-symbol-name will do what I want.

the code for the routine is:

(setq fn (open (getfiled "Topo File" (getvar "dwgprefix") "dca" 4) "r")) (while (setq x (read-line fn)) (setq x (read (strcat "(" x ")"))) (setq x1 (nth 1 x)) (setq y1 (nth 2 x)) (setq z1 (nth 3 x)) (setq lid (nth 4 x)) (print lid) )
(setq fn (close fn))

The text file looks like:

1 185198.42 85833.29 12.34 34_10 TC
2 185182.75 85830.08 12.34 34_11 TC
3 185181.57 85827.39 12.34 34_14 TC
4 185183.28 85825.06 12.34 34_11 TC

This is survey info.
Note that the 34_10 items are strings, but the read function returns them as variables. I would never parse text this way but a friend asked me how to do it.

So I can do an (setq lid (vl-symbol-name (list (nth 4 x)))) and it should give the string.

This is very bad code in my view, but unfortunately works. I'm sure to hear from that person when someone puts a space in the decription code...
thanks all

James Maeding
Civil Engineer/Programmer
 

Latest threads

Back
Top