Nullifying the declared variables automatically?

Discussion in 'AutoCAD' started by RaghuMN, Apr 8, 2004.

  1. RaghuMN

    RaghuMN Guest

    I have a program wherein there are 100s of variable declared, but not localised.

    When this program is executed, all the variables are left as it is in the memory without any cleaning.

    Is there any way to 'nil' all the variables automatically, WITHOUT listing out each variable manually in the defun (..) area or from a separate statement by as (setq variable nil)?

    My expectation is an idea towards a sub program that automatically finds all the declared variables still having values, and making them nil.

    Can anyone help me in this aspect?

    Thanks,
    MNRaghu
     
    RaghuMN, Apr 8, 2004
    #1
  2. RaghuMN

    hendie Guest

    isn't this what GC is for ?
     
    hendie, Apr 8, 2004
    #2
  3. RaghuMN

    Tom Smith Guest

    isn't this what GC is for ?

    No, not at all.
     
    Tom Smith, Apr 8, 2004
    #3
  4. RaghuMN

    Doug Broad Guest

    It is the responsibility of the programmer to manage the
    symbols that he uses. If variables are not localized, they
    must be specifically set to nil if that is the desired action.

    Any non-local symbols increase the risk that names will
    replace other user defined functions or variables.

    When a drawing is closed and reopened, the variables
    no longer exist. (GC) does not clean up non-localized
    variables.

    There is no shortcut to performing this operation other
    than storing the names in a list and then using something
    like:

    (foreach n lst (set n nil))




    from a separate statement by as (setq variable nil)?
     
    Doug Broad, Apr 8, 2004
    #4
  5. RaghuMN

    hendie Guest

    yup, I realised that after I posted but there's no way to edit posts in this forum.
     
    hendie, Apr 9, 2004
    #5
  6. RaghuMN

    CAB2k Guest

    I have not seen a routine, but ther should be one to run through the lisp
    code & print a list of variable names used so you could easily put them
    in the declared variable area.

    Anyone seen such code?

    CAB
     
    CAB2k, Apr 9, 2004
    #6
  7. RaghuMN

    BillZ Guest

    RaghuMN,
    What I have done to remedy this problem.
    Before you run your progam:

    (setq ltt (atoms-family 1))

    Then run your program and:

    (setq lts (atoms-family 1))

    (mapcar '(lambda (x)(if (not (member x ltt))(setq varlst (cons x
    varlst)))) lts)

    Now you have a list of all the new vars created by the program.
    Then:
    (mapcar '(lambda (x)(set (read x) nil)) varlst)

    Should do it.

    Bill
     
    BillZ, Apr 9, 2004
    #7
  8. RaghuMN

    Jeff Mishler Guest

    Sure, it's called "Check edit window" in the VLIDE. Yes, I know it
    returns some standard definitions (such as :vlax-false), you just need
    to be aware of what SHOULD be there and remove items that shouldn't
    (anything colored in blue is a hint), including the vars you WANT
    global.....

    Jeff
     
    Jeff Mishler, Apr 9, 2004
    #8
  9. RaghuMN

    Dan Allen Guest

    Where is "Check edit window" in the VLIDE and how does that show local vs.
    global variables? Per the help, blue indicates "Built-in functions and
    protected symbols", not locally defined variables?

    Dan
     
    Dan Allen, Apr 9, 2004
    #9
  10. RaghuMN

    Jeff Mishler Guest

    I didn't say it did, in fact I specifically say you need to
    remove the globals:
    "including the vars you WANT global....."
    As I said, I know this BUT the VLIDE incorrectly lists them as
    variables.
    I answered CAB with EXACTLY what he was asking:
    "print a list of variable names used so you could easily put them
    in the declared variable area."

    Here's a sample lisp (I know it isn't really anything, it's a sample for
    use here only) and the results, shown in the Build Output Window, of
    running "Check Edit Window", which, BTW, can also be called from the
    menu like this "Tools-Check Text in Editor", or by pressing CTRL-ALT-C.

    (defun c:test ()
    (setq aa (getreal "\nNumber to use: ")
    bb (getreal "\nNumber to use: ")
    cc (getreal "\nNumber to use: ")
    mynewVar (getreal "\nNumber to use: ")
    )
    (if (= cc :vlax-true)(exit))
    )

    Build output:

    [CHECKING TEXT <Untitled-3> loading...]
    ..
    ; === Top statistic:
    ; Global variables: :)vlax-true AA BB CC MYNEWVAR)
    ; Function definition (with number of arguments): ((C:TEST . 0))
    ; Check done.

    HTH,
    Jeff
     
    Jeff Mishler, Apr 9, 2004
    #10
  11. While it's easy and tempting to write an atoms-family cleaner
    (ie null out the variables) I think it's safer to write something
    to find all the globals, then edit your code accordingly.

    To witt, quick and dirty:
    Code:
    (defun FindGlobals ( )
    (vl-sort
    (vl-remove-if 'null
    (mapcar
    '(lambda (pair / typex)
    (if
    (and
    (null
    (member
    (setq typex (type (eval (car pair))))
    '(exrxsubr pagetb subr usubr)
    )
    )
    (null
    (wcmatch (cdr pair)
    ;; you may have to modify this filter to suit
    "AC*,C:*,PI,T,:VLAX*,VLAX*,:VLR*"
    )
    )
    )
    (cons
    (car pair)
    typex
    )
    )
    )
    (mapcar 'cons
    (atoms-family 0)
    (atoms-family 1)
    )
    )
    )
    '(lambda (a b)
    (<
    (vl-symbol-name (car a))
    (vl-symbol-name (car b))
    )
    )
    )
    )
    Calling this function like ...

    (mapcar 'print (findglobals))

    Might return something like this ...

    ....
    (*APP* . VLA-OBJECT)
    (*ATOMACTIVE* . STR)
    (*ATOMFILTER* . STR)
    (*BEGINSAVE* . LIST)
    (*CALLBACKS* . LIST)
    (*DOC* . VLA-OBJECT)
    (*DWGFILEOPEN* . LIST)
    (*ERRCODES* . LIST)
    (*EVENTS* . LIST)
    (*INFINITY* . REAL)
    (*LAST-VALUE* . SYM)
    (*METHODS* . LIST)
    (*PROPERTIES* . LIST)
    (*REACTORS* . LIST)
    ....

    Cheers :)

    from a separate statement by as (setq variable nil)?
     
    michael puckett, Apr 9, 2004
    #11
  12. This of course is flawed because a global like ACCOUNTING
    would not be listed (matches "AC*") but the general idea
    is sound; I just didn't want to list all the 650+ ac*
    AutoCAD global constants ... :)

    <snip>
     
    michael puckett, Apr 9, 2004
    #12
  13. RaghuMN

    Dan Allen Guest

    Ahh, I see now. You have to check the option Tools > Environments Options >
    General Options > Diagnostic Tab > Report statistics during syntax checking,
    othewise you get nada.

    Dan
    --
    ;;; For reply, change numbers to decimal


     
    Dan Allen, Apr 10, 2004
    #13
  14. RaghuMN

    CAB2k Guest

    Thanks for the tip Jeff.
    Don I tried the setting but still no variables.
    Still missing a switch I fear.

    [CHECKING TEXT DeleteFrezLayer.lsp loading...]
    ..
    ; === Top statistic:
    ; Global variables: :)vlax-false :vlax-true)
    ; Function definition (with number of arguments): ((C:FROZENLAYERKILLER . 0))
    ..
    ; === Top statistic:
    ; Function definition (with number of arguments): ((SSGET->VLA-LIST . 1))
    ..
    ; === Top statistic:
    ; Function definition (with number of arguments): ((VL-DELETEOBJECT . 1))
    ; Check done.
     
    CAB2k, Apr 10, 2004
    #14
  15. RaghuMN

    CAB2k Guest

    You could run "FindGlobals" , then step through the list it returns and nil all the variables.
    Some filtering my be needed.
     
    CAB2k, Apr 10, 2004
    #15
  16. ok.
     
    michael puckett, Apr 10, 2004
    #16
  17. RaghuMN

    CAB2k Guest

    Sorry for the laps, I wasn't following Michael's routine.
    But BillZ seems to have a doable solution.
     
    CAB2k, Apr 10, 2004
    #17
  18. I too thought BillZ suggestion was best <... was my initial
    thought:

    http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&selm=7p47qp$

    or

    http://tinyurl.com/29vcr

    ....> but Bill had already posted it, so I came up with (what I
    thought was) a fun alternative (incurable knurd here).

    As I had said, I think nulling out the vars automagically via code
    after the fact is not a good idea -- at best it's a lazy, sloppy
    way to clean up after one's self, and encourages poor form.

    Find the vars, fix the code.

    IMO.
     
    michael puckett, Apr 10, 2004
    #18
  19. RaghuMN

    CAB2k Guest

    agreed
     
    CAB2k, Apr 10, 2004
    #19
  20. ; Original source from: R. Robert Bell, MCSE

    (setq *TempLeak* nil) ; wipe on (re)loads

    (defun C:ALE_Leak (/ lVars orgAtoms)
    (cond
    ( *TempLeak*
    (setq orgAtoms *TempLeak*)
    (princ "\nRicerca atomi non-localizzati...")
    (foreach atomVar (atoms-family 1)
    (ALE_FUZZY nil)
    (cond
    ( (member atomVar orgAtoms)
    (setq orgAtoms (vl-remove atomVar orgAtoms))
    )
    ( (wcmatch atomVar ; modify this filter to suit
    "`#*,C:*,ALE_*,ALEAX_*,MSW_*,MSX_*,MSXP-*,MSXM-*,MSXC-*")
    )
    ( (setq lVars (cons atomVar lVars)) )
    )
    )
    (princ " fatto.\nAtomi non-localizzati: ")
    (if lVars
    (foreach atomVar lVars
    (princ (strcat atomVar " "))
    )
    (princ "nessun atomo trovato.")
    )
    )
    (T
    (princ "\nInizializza la lista degli atomi... ")
    (setq *TempLeak*
    (append
    '("*TEMPLEAK*" "ORGATOMS" "#SPIN" "*LAST-VALUE*")
    (atoms-family 1)
    )
    )
    (princ " fatto.")
    )
    )
    (princ)
    )

    (defun ALE_FUZZY (Countr /)
    (if Countr
    (progn
    (princ "Elaborazione in corso... ")
    (while (> Countr 1)
    (princ (strcat (chr 8) "|")) (princ)
    (princ (strcat (chr 8) "/" )) (princ)
    (princ (strcat (chr 8) "-")) (princ)
    (princ (strcat (chr 8) "\\")) (princ)
    (setq Countr (1- Countr))
    )
    (prompt "\n \n \n ")
    );progn
    (progn
    (or #spin (setq #spin "Elaborazione in corso... -"))
    (princ (strcat (chr 8) #spin)) (princ)
    (cond
    ( (equal #spin "-") (setq #spin "\\") )
    ( (equal #spin "\\") (setq #spin "|" ) )
    ( (equal #spin "|") (setq #spin "/" ) )
    ( T (setq #spin "-") )
    )
    );progn
    );if
    (princ)
    );defun ALE_FUZZY



    --
    ________________________________________________

    Marc'Antonio Alessi (TV) Italy
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    O.S. = XP Pro 2002 Ita - Sp.1
    AutoCAD = 2004 Ita - Sp.1a
    ________________________________________________
     
    Marc'Antonio Alessi, Apr 11, 2004
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.