+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Full Member benhubel's Avatar
    Computer Details
    benhubel's Computer Details
    Operating System:
    Windows 7
    Discipline
    Manufacture
    benhubel's Discipline Details
    Occupation
    Waterjet Programmer
    Discipline
    Manufacture
    Using
    AutoCAD 2012
    Join Date
    Dec 2015
    Location
    Dallas, Texas
    Posts
    78

    Default Define generated variables locally

    Registered forum members do not see this ad.

    I have been experimenting with generating variables through code rather than defining them manually. The problem is that when the program creates them, they are defined as global variables. Is there a way to generate them as local variables instead?

    Below is the sample code that I wrote for testing it.

    Code:
    ;ListToVariables creates variables named testvar0, testvar1, testvar2, etc.
    ;Each newly created variable contains the contents of the correlated slot from testlist.
    (defun c:test ( / testlist )
    	(setq testlist (list "aaa" "bbb" "ccc" "ddd" "eee"))
    	(ListToVariables testlist)
    )
    
    (defun ListToVariables ( listname / i )
    	(setq i 0)
    	(repeat (length listname)
    		(set (read (strcat "testvar" (rtos i 2 0))) (nth i listname))
    		(setq i (1+ i))
    	)
    	(princ)
    )
    I just now discovered for the first time that I am prone to memory loss.

  2. #2
    Forum Deity David Bethel's Avatar
    Discipline
    Multi-disciplinary
    David Bethel's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Commercial Food Service
    Using
    AutoCAD pre 2000
    Join Date
    Dec 2003
    Location
    Newport News, Virginia
    Posts
    2,531

    Default

    You can but it is a RPIA

    Your best bet is to use a standard name convention for these types that will be used for that session only

    I use a prefix of gv_ for variable that are global and set for reuse and tv_ for temp variables

    -David
    R12 (Dos) - A2K

  3. #3
    Full Member benhubel's Avatar
    Computer Details
    benhubel's Computer Details
    Operating System:
    Windows 7
    Discipline
    Manufacture
    benhubel's Discipline Details
    Occupation
    Waterjet Programmer
    Discipline
    Manufacture
    Using
    AutoCAD 2012
    Join Date
    Dec 2015
    Location
    Dallas, Texas
    Posts
    78

    Default

    Quote Originally Posted by David Bethel View Post
    You can but it is a RPIA

    Your best bet is to use a standard name convention for these types that will be used for that session only

    I use a prefix of gv_ for variable that are global and set for reuse and tv_ for temp variables

    -David
    That makes sense, and it sounds like the route I might have to go. Out of curiosity, I'm still interested to see how generated local variables could be done though. If anybody has any examples, or a link to any sort of documentation describing it, I'd love to check it out. The only way I can think of right now is to write it as an intermediate program that writes its own function which declares the variables locally.
    I just now discovered for the first time that I am prone to memory loss.

  4. #4
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,124

    Default

    Registered forum members do not see this ad.

    Maybe...

    Code:
    ; (ListToVariables "hello" '(44 55 88))
    (defun ListToVariables ( pref L / varnm i r )
      (setq i 0)
      (foreach x L
        (set (read (setq varnm (strcat pref (itoa (setq i (1+ i)))))) x)
        (setq r (cons varnm r))
      )
      (reverse r)
    )
    Code:
    (ListToVariables "hello" '(44 55 88)) >> ("hello1" "hello2" "hello3")
    hello1 >> 44
    hello2 >> 55
    hello3 >> 88
    hello4 >> nil

Similar Threads

  1. control layers appearance locally
    By samifox in forum AutoCAD General
    Replies: 11
    Last Post: 23rd Oct 2012, 11:51 am
  2. Save plot style locally
    By jgbgod in forum AutoCAD General
    Replies: 2
    Last Post: 27th Jan 2012, 03:56 pm
  3. Replies: 0
    Last Post: 13th Jan 2011, 02:30 am
  4. Hotfix - Refresh From Vault overwrites locally saved files
    By Autodesk in forum AutoCAD RSS Feeds
    Replies: 0
    Last Post: 23rd Nov 2010, 08:20 am
  5. If an AUGI CAD Camp is available locally, will you attend in 2010?
    By CAD Panacea in forum AutoCAD RSS Feeds
    Replies: 3
    Last Post: 14th Jan 2010, 07:38 am

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts