Author Topic: Is SET a misunderstood function?  (Read 5662 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Is SET a misunderstood function?
« on: December 07, 2005, 12:37:44 PM »
Or is there not a great need for it?

Code: [Select]
(setq lst '("one" "two" "three"))

(foreach i lst
  (set (read i) (getstring T "\nEnter a string: "))
  )

Code: [Select]
Enter a string: yes
Enter a string: no
Enter a string: maybe

Code: [Select]
(foreach i lst
  (princ (strcat "\n" (eval (read i))))
  )

Code: [Select]
yes
no
maybe
"\nmaybe"

TheSwamp.org  (serving the CAD community since 2003)

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Is SET a misunderstood function?
« Reply #1 on: December 07, 2005, 12:48:24 PM »
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
« Last Edit: December 07, 2005, 01:45:16 PM by Jeff_M »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Is SET a misunderstood function?
« Reply #2 on: December 07, 2005, 01:06:58 PM »
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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Is SET a misunderstood function?
« Reply #3 on: December 07, 2005, 01:16:36 PM »
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
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

LE

  • Guest
Re: Is SET a misunderstood function?
« Reply #4 on: December 07, 2005, 01:21:02 PM »
I think I have use it no more than 10 times... since I got into the lisp mania.....

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is SET a misunderstood function?
« Reply #5 on: December 07, 2005, 01:29:02 PM »
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  ^^
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Is SET a misunderstood function?
« Reply #6 on: December 07, 2005, 01:34:26 PM »
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).
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Is SET a misunderstood function?
« Reply #7 on: December 07, 2005, 01:40:03 PM »
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
Code: [Select]
(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
  • and see it's value


I am sure there are more elegant ways to use it, but this was just one that I used many years ago ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is SET a misunderstood function?
« Reply #8 on: December 07, 2005, 01:44:12 PM »
Carefull with the scare word Michael.  :lol:  :lol:

Sorry Dent, couldn't help myself !
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Is SET a misunderstood function?
« Reply #9 on: December 07, 2005, 01:46:38 PM »
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??
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is SET a misunderstood function?
« Reply #10 on: December 07, 2005, 01:58:19 PM »
Just ignore it MP. My weird sense of humour (sic) . :)





kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is SET a misunderstood function?
« Reply #11 on: December 07, 2005, 02:14:08 PM »
Some < generally uncommented> examples

Code: [Select]
  ;;----- 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)
  )

Code: [Select]
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
;;; 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)
)

Code: [Select]
;;;------------------------------------------------------------------
;;;
;;; 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)
       )
  )
)

Code: [Select]

(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)
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is SET a misunderstood function?
« Reply #12 on: December 07, 2005, 02:21:50 PM »
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))
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

whdjr

  • Guest
Re: Is SET a misunderstood function?
« Reply #13 on: December 07, 2005, 04:03:33 PM »
I have used it for sysvars:

Code: [Select]
;;;(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.

Code: [Select]
  (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
Code: [Select]
  (mapcar 'set
  '(tar_files src_files)
  (mapcar 'get_files flist (list str str))
  )
and
Code: [Select]
      (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.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Is SET a misunderstood function?
« Reply #14 on: December 07, 2005, 08:00:06 PM »
Or is there not a great need for it?

Code: [Select]
(setq lst '("one" "two" "three"))

(foreach i lst
  (set (read i) (getstring T "\nEnter a string: "))
  )

Code: [Select]
Enter a string: yes
Enter a string: no
Enter a string: maybe

Code: [Select]
(foreach i lst
  (princ (strcat "\n" (eval (read i))))
  )

Code: [Select]
yes
no
maybe
"\nmaybe"



try this...

Code: [Select]
(foreach i lst
  (princ (strcat "\n" (eval (read i))))
  (princ)
  )
Keep smile...