Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Changing the elements in a list with strcat

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
dmfrazier
532 Views, 6 Replies

Changing the elements in a list with strcat

Using AutoLISP, I have the following list set to a variable "dwginfo" (the list was generated using the DOSLib function "dos_getfilem"):


("C:\\Drawing\\Temp\\000aaTest\\DwgConvert\\" "test1.dwg" "test1x.dwg" "test2.dwg" "test2x.dwg" "test3.dwg" "test4.dwg")

 

I take the first element and store it as "path":


Command: (setq path (car dwginfo))
"C:\\Drawing\\Temp\\000aaTest\\"

 

Then I remove the first element and store this new list as "names":


Command: (setq names (cdr dwginfo))
("test1.dwg" "test1x.dwg" "test2.dwg" "test2x.dwg" "test3.dwg" "test4.dwg")

 

Now, what I would like to do is create a new list consisting of each element in "names" with the path added to it (in front).

I have tried this with varying combinations of list and foreach, but the closest I can get is this:

 

Command: (setq dwgs (list (foreach nam names (strcat path nam))))
("C:\\Drawing\\Temp\\000aaTest\\test4.dwg")

 

Foreach only returns the results of the last concatenation, so that's all I get in the "list".

 

How can I build a list containing all of the concatenated names?

 

Thanks.

6 REPLIES 6
Message 2 of 7
hmsilva
in reply to: dmfrazier

(setq dwgs (mapcar '(lambda (x) (strcat path x))  names))

HTH
Henrique

EESignature

Message 3 of 7
Kent1Cooper
in reply to: dmfrazier

hmsilva's way is certainly more concise, but here's a way to re-arrange mostly the elements of yours to do it:

 

(foreach nam names (setq dwgs (cons (strcat path nam) dwgs)))

 

If the order needs to be the same as in the names list, you would then want to:

 

(setq dwgs (reverse dwgs))

Kent Cooper, AIA
Message 4 of 7
dmfrazier
in reply to: hmsilva

I had a feeling it would involve mapcar, but couldn't figure out how to use it.  And lambda has always been a mystery for me.

Thank you very much for that, Henrique.  It is exactly what I needed. 

Message 5 of 7
dmfrazier
in reply to: Kent1Cooper

I should've thought of cons.

Thanks, Kent.  It's always nice to have alternatives (even less concise ones).

Message 6 of 7
hmsilva
in reply to: dmfrazier


@dmfrazier wrote:

I had a feeling it would involve mapcar, but couldn't figure out how to use it.  And lambda has always been a mystery for me.

Thank you very much for that, Henrique.  It is exactly what I needed. 


You're welcome, dmfrazier
Glad I could help.

 

May be helpful this Lee Mac's explanation Mapcar & Lambda

 

Henrique

EESignature

Message 7 of 7
Jason.Piercey
in reply to: dmfrazier


@dmfrazier wrote:

 And lambda has always been a mystery for me.

 

There are differences but essentially, for most practical purposes, Lambda  is the same as defun without the need for a name.
defun = named function
lambda = nameless function

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost