Author Topic: Apply String prefix to all strings in a list  (Read 121 times)

0 Members and 1 Guest are viewing this topic.

vincent.r

  • Mosquito
  • Posts: 12
Apply String prefix to all strings in a list
« 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" ...)

Lee Mac

  • Seagull
  • Posts: 11780
  • AutoCAD 2015 Windows 7 London, England
Re: Apply String prefix to all strings in a list
« Reply #1 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 tutorial.

vincent.r

  • Mosquito
  • Posts: 12
Re: Apply String prefix to all strings in a list
« Reply #2 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" ...)


Lee Mac

  • Seagull
  • Posts: 11780
  • AutoCAD 2015 Windows 7 London, England
Re: Apply String prefix to all strings in a list
« Reply #3 on: September 09, 2017, 08:37:47 am »
Code - Auto/Visual Lisp: [Select]
  1. (mapcar 'strcat list2 list1)

 :-)

vincent.r

  • Mosquito
  • Posts: 12
Re: Apply String prefix to all strings in a list
« Reply #4 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

Grrr1337

  • Bull Frog
  • Posts: 355
Re: Apply String prefix to all strings in a list
« Reply #5 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)))

MP

  • Seagull
  • Posts: 16985
  • brevity != aggression
Re: Apply String prefix to all strings in a list
« Reply #6 on: September 09, 2017, 02:03:45 pm »
Visualize what's happening inside out -- one statement at at a time.
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Grrr1337

  • Bull Frog
  • Posts: 355
Re: Apply String prefix to all strings in a list
« Reply #7 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!

MP

  • Seagull
  • Posts: 16985
  • brevity != aggression
Re: Apply String prefix to all strings in a list
« Reply #8 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.
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About