+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Full Member
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    45

    Default How to get a substring of a variable which was input for my defun

    Registered forum members do not see this ad.

    Hello everyone, I hope I have a simple issue. But I couldn't find a clue how to solve it.

    Code:
    (setq CCLIST (list "aa" "ab" "ac"))
    
    (defun MJO:whatever ( MLIST\ )
     (set (read(strcat "ANO" (substr MLIST 1 2))) 1)
    )
    What I would like to get is a variable ANOCC to be set to 1 when I run

    (MJO:whatever CCLIST) in command bar.

    But of yourse it is interpreting MLIST in the substr line.

    How could I get the correct variable name?

  2. #2
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    949

    Default

    You cant use SUBSTR on a list ? You also need to change this: ( MLIST\ ) to ( MLIST )

    If I'm understanding you look into MEMBER (case sensitive).. something like this although I'm still not sure of the logic here.

    Code:
    (setq cclist '("aa" "ab" "ac"))
    (defun _whatever (l k v / i)
      (if (setq i (car (member k cclist)))
        (set (read (strcat "ANO" i)) v)
      )
    )
    (_whatever cclist "ab" 0.111)

  3. #3
    Full Member
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    45

    Default

    Hi Ronjonp,

    with your code I get a symbol name assembled from the fixed string "ANO" and a member of the list. Thats not what I try to achieve.

    The routine will run through dozens of lists and I want to be able to identify the correct variable for each of the list by the name of it. Therefore I want the fixed string "ANO" and the first two characters of the list which was given when the defun was called.

    Lets say I have 3 lists :
    Code:
    (setq CCLIST '("tmp1" "text2" "something"))
    (setq PCLIST '("anything" "tmp2"))
    (setq YCLIST '("word1" "name" "text346" "demo"))
    And now it should do something with this lists. (I just skiped this part to make it less confusing) in our exapmle it could be that I want to store the number of entries.
    Code:
    (defun MJO:whatever (MLIST / ) 
    (set (read (strcat "ANO" *CodeIamMissing*)) (length MLIST))
    )
    
    (MJO:whatever CCLIST)
    (MJO:whatever PCLIST)
    (MJO:whatever YCLIST)
    What I would like to have afterwards should be:

    ANOCC = 3
    ANOPC = 2
    ANOYC = 4

    I'm looking for the *CodeIamMissing* part in the defun above. Is there something?

    Later in the code this numbers will be used to set the integer for the repeat comand or to be written into a table. That is why I need to be able to identify them.

  4. #4
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    1,106

    Default

    this???
    Code:
    (defun tst ( / CCLIST PCLIST YCLIST)
      (vl-load-com)
      (setq CCLIST '("tmp1" "text2" "something"))
      (setq PCLIST '("anything" "tmp2"))
      (setq YCLIST '("word1" "name" "text346" "demo"))
      (MJO:whatever 'CCLIST)
      (princ)
    )
    (defun MJO:whatever (MLIST / name value)
      (setq name (strcat "ANO" (vl-symbol-name MLIST)))
      (setq value (length (vl-symbol-value MLIST)))
      (set (read name) value)
      (princ "\nname = ")(princ name)(princ " , value = ")(princ (eval (read name)))
      (princ)
    )

  5. #5
    Full Member
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    45

    Default

    Thank you rlx. I guess my first atempt was half way done. I didn't know about (vl-symbol-value). It helps a lot!

    Topic is solved. Thanks again!

  6. #6
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    1,106

    Default

    nos problemos

  7. #7
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Win 7 Pro / Win 10 Pro / Linux Mint
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2018
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,568

    Default

    I would suggest passing the value of the list variable to your function and then assign the value returned by your function to your new variable, for example:
    Code:
    (defun c:test ( / ANOCC ANOPC ANOYC CCLIST PCLIST YCLIST )
        (setq CCLIST '("tmp1" "text2" "something")
              PCLIST '("anything" "tmp2")
              YCLIST '("word1" "name" "text346" "demo")
              ANOCC   (do-something CCLIST)
              ANOPC   (do-something PCLIST)
              ANOYC   (do-something YCLIST)
        )
        (princ)
    )
    
    (defun do-something ( lst )
        (length lst) ;; Return the length of the list, but could be anything
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  8. #8
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    949

    Default

    Quote Originally Posted by Ament View Post
    Hi Ronjonp,

    with your code I get a symbol name assembled from the fixed string "ANO" and a member of the list. Thats not what I try to achieve.

    The routine will run through dozens of lists and I want to be able to identify the correct variable for each of the list by the name of it. Therefore I want the fixed string "ANO" and the first two characters of the list which was given when the defun was called.

    Lets say I have 3 lists :
    Code:
    (setq CCLIST '("tmp1" "text2" "something"))
    (setq PCLIST '("anything" "tmp2"))
    (setq YCLIST '("word1" "name" "text346" "demo"))
    And now it should do something with this lists. (I just skiped this part to make it less confusing) in our exapmle it could be that I want to store the number of entries.
    Code:
    (defun MJO:whatever (MLIST / ) 
    (set (read (strcat "ANO" *CodeIamMissing*)) (length MLIST))
    )
    
    (MJO:whatever CCLIST)
    (MJO:whatever PCLIST)
    (MJO:whatever YCLIST)
    What I would like to have afterwards should be:

    ANOCC = 3
    ANOPC = 2
    ANOYC = 4

    I'm looking for the *CodeIamMissing* part in the defun above. Is there something?

    Later in the code this numbers will be used to set the integer for the repeat comand or to be written into a table. That is why I need to be able to identify them.
    I'm glad you got an answer but I'm still confused on the logic. I can't think where I'd ever structure code this way.

  9. #9
    Full Member
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    45

    Default

    Registered forum members do not see this ad.

    Hi LeeMac, also a good Idea. Thank you for that proposal. But I'll stay with rlx. The issue is that inside the "do-something" I'm calculating more than 10 different values of Blocks and their attributes. If I use your proposal I have to make a subroutine for each value or get a list from it which needs to be separated later. I imagine that my code will be even longer with that . I'll have to use nth excessively later in the code.


    Hi ronjonp the issue here is that the user is asked to select a project he's working on. This choise is a switch which lists are taken into account. With the correct choise the subroutine will be startet up to 50 times for different lists. The lists contain some identification strings of blocks which are selectet via ssget and searched for dedicated attributes like their position, x/y/z-direction factors, layer and several custom attributes. Later in the code each blocks position with the same identification sting is checked against each other to avoid that there is the same block stacked on top of each other and the attributes are taken into account to fill a table similar to the "extract data" command.



    The issue with that is that it takes into account the whole block names when it comes to sums and not just a small identificator. It could be that there are e.g. the same kind of table with different shapes of the plate. So this table's block names will be "Table-oak-L-01","Table-oak-L02" and "Table-oak-S01". I need to group all of them together and will use the string "Table-oak*" as identificator in the list.

Similar Threads

  1. defun inside another defun
    By samifox in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 7th Apr 2014, 05:43 am
  2. Replies: 6
    Last Post: 19th Oct 2012, 02:55 am
  3. Limiting the input value of a variable
    By aloy in forum AutoLISP, Visual LISP & DCL
    Replies: 11
    Last Post: 22nd Jul 2012, 01:18 am
  4. Set getstring variable using a DCL input box?
    By Brick_top in forum AutoLISP, Visual LISP & DCL
    Replies: 12
    Last Post: 5th Jan 2012, 03:26 pm
  5. Extracting a substring of text
    By jda823 in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 3rd Dec 2010, 03:21 pm

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