Jump to content
Ament

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

Recommended Posts

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

 

(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?

Share this post


Link to post
Share on other sites

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. :?

 

(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)

Share this post


Link to post
Share on other sites

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 :

(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.

(defun MJO:whatever (MLIST / ) 
(set (read (strcat "ANO" [b]*CodeIamMissing*[/b])) (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.

Share this post


Link to post
Share on other sites

this???

(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)
)

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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:

(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
)

Share this post


Link to post
Share on other sites
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 :

(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.

(defun MJO:whatever (MLIST / ) 
(set (read (strcat "ANO" [b]*CodeIamMissing*[/b])) (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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×