TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: vincent.r on September 09, 2017, 06:56:01 AM

Title: Apply String prefix to all strings in a list
Post by: vincent.r on September 09, 2017, 06:56:01 AM
Is it possible to apply a string prefix to the all strings in a list without using while function ?

want to apply string "x." to the list ("dwg" "dwgname" "dwgnumber" ...)

output will be ("x.dwg" "x.dwgname" "x.dwgnumber" ...)
Title: Re: Apply String prefix to all strings in a list
Post by: Lee Mac on September 09, 2017, 07:33:24 AM
mapcar is the key here, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar '(lambda ( x ) (strcat "x." x)) '("dwg" "dwgname" "dwgnumber"))
  2. ("x.dwg" "x.dwgname" "x.dwgnumber")

To understand what's happening here, you might be interested in my Mapcar & Lambda (http://lee-mac.com/mapcarlambda.html) tutorial.
Title: Re: Apply String prefix to all strings in a list
Post by: vincent.r on September 09, 2017, 08:29:08 AM
Thanks Lee, earlier I tried with your tutorial but I was mistaking somewhere or I was doing it in a wrong way.

One more trouble. I want to use strcat function for two lists.

e.g. list1 ("dwg" "dwgname" "dwgnumber" ...)
      list2 ("x." "y." "z." ...)

output will be ("x.dwg" "y.dwgname" "z.dwgnumber" ...)

Title: Re: Apply String prefix to all strings in a list
Post by: Lee Mac on September 09, 2017, 08:37:47 AM
Code - Auto/Visual Lisp: [Select]
  1. (mapcar 'strcat list2 list1)

 :-)
Title: Re: Apply String prefix to all strings in a list
Post by: vincent.r on September 09, 2017, 09:39:41 AM
Ohhh. Its very easy. I need work again on mapcar and lambda function to understand it.

Thanks Lee
Title: Re: Apply String prefix to all strings in a list
Post by: Grrr1337 on September 09, 2017, 01:49:03 PM

And I'm leaving one frustrating example that can be found in Lee's website:

Code - Auto/Visual Lisp: [Select]
  1. (apply 'mapcar (cons 'strcat (list list1 list2)))
Title: Re: Apply String prefix to all strings in a list
Post by: MP on September 09, 2017, 02:03:45 PM
Visualize what's happening inside out -- one statement at at a time.
Title: Re: Apply String prefix to all strings in a list
Post by: Grrr1337 on September 09, 2017, 02:21:28 PM
Visualize what's happening inside out -- one statement at at a time.

I just started playing/experimenting with this techique,
also remember Lee exampling how the function (in this example is strcat) applies to every nth item in every provided sublist.
I might post some ideas after the hurricane.

Yea I know there's no word 'exampling' but it sounds cool!
Title: Re: Apply String prefix to all strings in a list
Post by: MP on September 09, 2017, 02:23:01 PM
For simplicity ...

(setq
    list1 '("a" "b")
    list2 '("1" "2")
)

(list list1 list2)
>> (("a" "b") ("1" "2"))

(cons 'strcat (("a" "b") ("1" "2")))
>> (strcat ("a" "b") ("1" "2"))

(apply 'mapcar (strcat ("a" "b") ("1" "2")))
>> (mapcar 'strcat ("a" "b") ("1" "2"))

When executed yields:
>> ("a1" "b2")


Forgive any typos -- bashed out on my iphone.