TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on December 07, 2005, 12:37:44 PM
-
Or is there not a great need for it?
(setq lst '("one" "two" "three"))
(foreach i lst
(set (read i) (getstring T "\nEnter a string: "))
)
Enter a string: yes
Enter a string: no
Enter a string: maybe
(foreach i lst
(princ (strcat "\n" (eval (read i))))
)
yes
no
maybe
"\nmaybe"
-
I use it occasionaly, but I don't write code that requires it's use all that often.
Removed comment induced by lack of coffee
-
I don't use it that often. When I do it's when I pass a parameter quoted and want to mimic pass by reference / pointer (very infrequent) or want to set a series of variables to values in a list, for example coordinates.
-
Ok, simplified examples ...
(defun foo ( QuotedArgument )
(set QuotedArgument 42)
)
(setq x 0)
(foo 'x)
!x => 42
(mapcar 'set '(x y z) '(1 2 3))
!x => 1
!y => 2
!z => 3
-
I think I have use it no more than 10 times... since I got into the lisp mania.....
-
Hi Mark
I used to use it a lot with DCL programming. Kept a list of key names. Then kept a global associated list of key names and values for each dialog run. Using set made it easy to produce variables with the same 'name' as the key.
I've been using ObjectDCL for a while now so dont use that methodology as much, and mainly use set now when reading variable names and values from ini type files or databases, or as indicated by MP ^^
-
You scare me Kerry! I've done the same with databases, i.e. map field names to attribute names (if I've read between the lines correctly) and I recall a dcl app that I made for someone (a phone listing with a button for each letter).
-
When I was writing large dynamic programs I would use SET almost as often as SETQ .. the reason is that I could define multiple variables on the fly based on user input and I didn't have to store the items in a sorted list. I also used it to mimic variable length char arrays much like in C++ ... for example in one particular function I remember that I would use
(setq myvar[] "thisvalue")
;and then
(repeat (strlen myvar[])
(setq ndx 1)
(set (read (strcat "myvar[" (rtos ndx 2 0) "]"))(substr myvar[] ndx 1))
(setq ndx (1+ ndx))
)
Now at some point I can check myvar
I am sure there are more elegant ways to use it, but this was just one that I used many years ago ...
-
Carefull with the scare word Michael. :lol: :lol:
Sorry Dent, couldn't help myself !
-
Carefull with the scare word Michael. :lol: :lol:
Sorry Dent, couldn't help myself !
With or without the Dent reference I don't get it.
Eh??
-
Just ignore it MP. My weird sense of humour (sic) . :)
-
Some < generally uncommented> examples
;;----- Release Bound Activex Objects --------------------------
(foreach varname kbsg:objectsbound
(if (= (type (setq tmp (vl-symbol-value varname))) 'vla-object)
(if (not (vlax-object-released-p tmp))
(vlax-release-object tmp)
)
)
(set varname nil)
)
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
;;; a general utility -- pop first element permenantly from quoted list
;;; and return this element's value
;;; example : using previuusly set tmp_list
;;; (kbsf:list-pop 'tmp_list) ==> ("EXPERT" . 2)
;;; tmp_list now= (("BLIPMODE" . 0) 1 "TESTSTRING")
(defun kbsf:list-pop (quoted_lstsym / tmp)
(setq tmp (eval quoted_lstsym))
(set quoted_lstsym (cdr tmp))
(car tmp)
)
;;;------------------------------------------------------------------
;;;
;;; a general utility -- replace cons list from assoc list
;;; say tmp_list is= (("EXPERT" . 2) ("TEXTSIZE" . 3.5) ("BLIPMODE" . 0))
;;; (kbsf:replace-assoc "TEXTSIZE" 5.0 'tmp_list)
;;; ==>(("EXPERT" . 2)("TEXTSIZE" . 5.0)("BLIPMODE" . 0))
;;; note : Removes first reference only
;;; to replace the last matching cons in list :
;;; (setq tmp_list (reverse tmp_list)
;;; tmp_list (reverse (kbsf:replace-assoc .... 'tmp_list)))
;;;
(defun kbsf:replace-assoc (assoctag assocval quoted_lst_sym)
(set quoted_lst_sym
(subst (cons assoctag assocval)
(assoc assoctag (eval quoted_lst_sym))
(eval quoted_lst_sym)
)
)
)
(defun kbsf:getclassmembers
(dbfile class / return temp idno ml memberlist)
(if (not (eval (read (strcat "gb:" class "_members"))))
(progn (setq temp (kbsf:sqldump dbfile class "description" nil)
idno (vl-position "description" (car temp))
memberlist (cdr temp)
ml '()
ml (foreach x memberlist (setq ml (cons (nth idno x) ml)))
)
(set (read (strcat "gb:" class "_members")) (reverse ml))
(set (read (strcat "gb:" class "_data")) temp)
)
)
(princ)
)
-
and this one I use a LOT, particularly when debugging .. I may have all variables temporarily global, and keep a global list of program variables.
.. use this to reset the variables to nil.
(foreach var *global-var-list (set (read var) nil))
-
I have used it for sysvars:
;;;(setq var_names '("OSMODE" "ORTHOMODE" "CMDECHO" "BLIPMODE")
;;; var_vars '(osm otm cmd bpm)
;;; var_vals '(0 1 0 0)
;;;)
;;;Usage: (store_old_vals var_vars var_names)
;;; (set_new_vals var_names var_vals)
;;; (restore_old_vals var_names var_vars)
;;; Restore also goes in the error handler.
(defun *store_old_vals* (vars names)
(mapcar 'set vars (mapcar 'getvar names))
)
(defun *set_new_vals* (names vals)
(mapcar 'setvar names vals)
)
(defun *restore_old_vals* (names vars)
(mapcar 'setvar names (mapcar 'eval vars))
)
;;;
But I really like to use it in nested mapcar's to set each item in a list to a variable.
(mapcar 'set
'(sfolder tfolder)
(mapcar '(lambda (x) (get_folder_path (get_folder_obj x)))
'("Select Source Folder"
"Select Target Folder to delete duplicate files from"
)
)
)
and
(mapcar 'set
'(tar_files src_files)
(mapcar 'get_files flist (list str str))
)
and
(mapcar
'set
'(tfolders sfolders)
(mapcar '(lambda (x) (mapcar '(lambda (y) (strcat x y)) same))
flist
)
)
These are probably basic examples of its use but I do like to use it.
-
Or is there not a great need for it?
(setq lst '("one" "two" "three"))
(foreach i lst
(set (read i) (getstring T "\nEnter a string: "))
)
Enter a string: yes
Enter a string: no
Enter a string: maybe
(foreach i lst
(princ (strcat "\n" (eval (read i))))
)
yes
no
maybe
"\nmaybe"
try this...
(foreach i lst
(princ (strcat "\n" (eval (read i))))
(princ)
)
-
try this...
why ?
-
Kerry, you're killing me man.
-
try this...
why ?
beacause seem to work.. :-D
-
I have used set in some programs.
Here is a tricky one.
This function is a generalized error trap. It can "wrap" a function and catch an error if one occurs and will return a nil when an error has occurred.
Like (vla-item colLayers "TEST") should return a the layer object corresponding to the "TEST" layer.
If there isn't a "TEST" layer it will crash.
so
(errortrap (quote (vla-item colLayers "TEST"))) would not crash and just return a nil. It would return the layer object if successfull.
The trick to using the set function is that when you use the vl-catch-all-apply you need to supply a list of arguments to apply the function to.
In this example, the argument that is passed is the result!
In order to do this you need the SET function!
Set is cool.
By the way it was nice to meet you face to face at AU Mark!
Peter Jamtgaard
; Standardized Error Trap
(defun ErrorTrap (symFunction / objError result)
(if (vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (X)(set X (eval symFunction)))
(list 'result))))
(progn
(if DEBUG
(progn
(princ "\n")
(princ (vl-catch-all-error-message objError))
(princ "\nWhile evaluating the expression: ")
(princ symfunction)
(princ "\n")
)
)
nil
)
(if result result 'T)
)
)
-
I use set a lot with variable length parameter passing
(defun myfoo (input)
(foreach a input
(set (read (substr a 1 1))
(atof (substr a 2)))))
(setq output '("X24" "Y30" "Z36"))
(myfoo output)
You can get very imaginative with some additional testing
("ALeft" "BRight" "CRight" "DRight" "GL" "HL" "IR" "JR" "LRear" "MCaster" "N8" "QSelfcontained" "RRefrigerator" "S2" "WWorktop" "PValueDesigner" "VAll" "X48" "Y30" "Z36")
-David