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

0 Members and 1 Guest are viewing this topic.

vincent.r

  • Newt
  • Posts: 101
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: 12912
  • 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

  • Newt
  • Posts: 101
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: 12912
  • 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

  • Newt
  • Posts: 101
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

  • Swamp Rat
  • Posts: 812
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)))
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
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!
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst