Author Topic: Using concatenated variable-names with 'foreach'  (Read 1273 times)

0 Members and 1 Guest are viewing this topic.

Peter2

  • Swamp Rat
  • Posts: 653
Using concatenated variable-names with 'foreach'
« on: March 21, 2015, 02:57:24 PM »
Maybe a crazy idea (or a simple with the right function ..), but I would like to know if variable-names can be used in a concatenated (and simple ...) form.

I have a code like this:
Code - Auto/Visual Lisp: [Select]
  1.      (if (= rad_AA 1)
  2.          (setq val_AA (+ top_AA  left_AA))
  3.      )
  4.      (if (= rad_BB 1)
  5.          (setq val_BB (+ top_BB  left_BB))
  6.      )
  7. .... and 20 more ...
Can it be done in a kind of loop like
Code - Auto/Visual Lisp: [Select]
  1. (foreach MyValue (list AA BB)    
  2. (if (= rad_MyValue 1)
  3.          (setq val_MyValue (+ top_MyValue  left_MyValue))
  4.      )
  5. )
Have a fine weekend.


Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Using concatenated variable-names with 'foreach'
« Reply #1 on: March 21, 2015, 03:40:05 PM »
Code - Auto/Visual Lisp: [Select]
  1. (foreach suf '("AA" "BB")
  2.     (if (= 1 (eval (read (strcat "rad_" suf))))
  3.         (set (read (strcat "val_" suf))
  4.              (+ (eval (read (strcat "top_"  suf)))
  5.                 (eval (read (strcat "left_" suf)))
  6.              )
  7.         )
  8.     )
  9. )

Though using lists is usually a far better approach.

Peter2

  • Swamp Rat
  • Posts: 653
Re: Using concatenated variable-names with 'foreach'
« Reply #2 on: March 21, 2015, 03:46:44 PM »
Hi Lee

thanks a lot.
Though using lists is usually a far better approach.
?? Can you gave a simple example?
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23