How do i access the variables that LISP generates.

I think I am missing something very simple here but I've been stuck for days.
I've managed to get LISP to generate variables each of which stores a list. But I don't know how to get the variables back.

(setq vcount '())
(setq idxylist '())
(setq idxy 1)
(setq coordbatch columns)
   (foreach xgroup coordbatch
   (set (read (strcat "xygroup" (itoa idxy))) xgroup)
   (setq vcount (strcat "xygroup" (itoa idxy)))
   (setq idxylist (cons vcount idxylist))

			(foreach entry xgroup
				(cons (assoc entry xylist) xygroup)
			)
(setq idxy (1+ idxy))
So now the variables "xygroup1" "xygroup2" etc. exist. And I've stored their names in a list variable called "idxylist".

But I can't get content using the "idxylist". I know that the varibles themselves are have lists. Because I can say (print xygroup1) and it will display the content of xygroup1.

I need to be able to cycle through all the variables that are created. But can't know how many will be created, so i need to be able to recover those variable names through code.

Best Answer

  • Jason Bourhill
    Accepted Answer
    You can use eval or vl-symbol-value to retrieve the value of a symbol. Using vl-symbol-value is recommended as it's significantly faster.

    e.g. here is your code turned into a function that returns a list of the variable names set
    (defun definevars (columns / vcount coordbatch  xgroup )
      (setq vcount '())
      (setq idxylist '())
      (setq idxy 1)
      (setq coordbatch columns)
      (foreach xgroup coordbatch
        (set (read (strcat "xygroup" (itoa idxy))) xgroup)
        (setq vcount (strcat "xygroup" (itoa idxy)))
        (setq idxylist (cons vcount idxylist))
    
    ;    (foreach entry xgroup
    ;      (cons (assoc entry xylist) xygroup)
    ;    )
        (setq idxy (1+ idxy))
      )
      idxylist
    )
    Calling with a list of values:
    : (setq varlist (definevars '(10 20 30 40 50 60 70)))
    ("xygroup7" "xygroup6" "xygroup5" "xygroup4" "xygroup3" "xygroup2" "xygroup1")
    Retrieving the values assigned to these variables
    : (mapcar '(lambda (v) (vl-symbol-value (read v))) varlist)
    (70 60 50 40 30 20 10)
    You can simplify this if you don't store the symbol names as strings.
    (defun definevars2 (coordbatch / xgroup idxy varnm idxylist )
      (setq idxy 1)
      (foreach xgroup coordbatch
        (setq varnm (read (strcat "xygroup" (itoa idxy)))) ; build var name
        (set varnm xgroup) ; set the created var name to a value
        (setq idxylist (append idxylist (list varnm ))) ; append the var name to a list
        (setq idxy (1+ idxy)) ; increment counter
      )
      idxylist
    )
    Calling with a list of values:
    : (setq varlist (definevars2 '(10 20 30 40 50 60 70)))
    (XYGROUP1 XYGROUP2 XYGROUP3 XYGROUP4 XYGROUP5 XYGROUP6 XYGROUP7)
    Retrieving the values assigned to these variables
    : (mapcar 'vl-symbol-value varlist)
    (10 20 30 40 50 60 70)
    Note I used append instead of cons to build my list in the same order as the provided list.

    Regards,
    Jason Bourhill
    BricsCAD V22 Ultimate
    CAD Concepts

Answers

  • Thanks that works !
  • Often with a list it will be a list of lists think excel rows, so if you use foreach you will get each row but how to get column values ? You can use the nth function eg (nth x lst) the x value starts at 0 not 1.

    lst ((1 2 3)(4 5 6).........

    so (nth 1 (nth 0 lst)) is 2
    If we look at a list of points XYZ ((23.45 67.89 0)(45.67 34.34 0.0) ........ can see patterns so can get x y z.

    If for some reason dont know how many items in a sub list can use nested foreach or repeat
    (foreach val lsts (foreach vals val)
    or (repeat (length val)
  • ALANH said:

    Often with a list it will be a list of lists think excel rows, so if you use foreach you will get each row but how to get column values ? You can use the nth function eg (nth x lst) the x value starts at 0 not 1.

    lst ((1 2 3)(4 5 6).........

    so (nth 1 (nth 0 lst)) is 2
    If we look at a list of points XYZ ((23.45 67.89 0)(45.67 34.34 0.0) ........ can see patterns so can get x y z.

    If for some reason dont know how many items in a sub list can use nested foreach or repeat
    (foreach val lsts (foreach vals val)
    or (repeat (length val)

    Funny you should mention that. I did indeed end up using foreach. I nested a foreach inside a foreach and did away with the variable name. Top points to Jason Bourhill for his perfect answer though. It will help me and many people.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!