TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: 2e4lite on August 07, 2014, 11:00:28 PM

Title: To get the string
Post by: 2e4lite on August 07, 2014, 11:00:28 PM
A known list:
 
Code: [Select]
'(("1" nil "France")("2" nil "Germany")("3" nil "Holland")("D" nil "Berne"))Is there a very easy and quick way to get the string to following?
 
Code: [Select]
"France(1)/Germany(2)/Holland(3)/Berne(D)/"Or
 
Code: [Select]
"France(1)/Germany(2)/Holland(3)/Berne(D)"
Title: Re: Is there a easy and quick way to get the string?
Post by: Kerry on August 07, 2014, 11:01:44 PM

What code have you tried ?
Title: Re: To get the string
Post by: 2e4lite on August 07, 2014, 11:04:29 PM
Hey
    This is my way:(vl-string-trim "()" (vl-princ-to-string (mapcar '(lambda (x) (strcat (last x) "(" (car x) ")/")) lst)))
    Is there a better way ?
Title: Re: To get the string
Post by: Kerry on August 07, 2014, 11:11:31 PM
This may be cleaner

Code - Auto/Visual Lisp: [Select]
  1. (apply 'strcat (mapcar '(lambda (x) (strcat (last x) "(" (car x) ")/")) lst))
Title: Re: To get the string
Post by: dgpuertas on August 08, 2014, 03:53:18 AM
or maybe

Code: [Select]
(strcat (caddar lst) "(" (caar lst) ")"
   (apply (function strcat) (mapcar (function (lambda (l) (strcat "/" (caddr l) "(" (car l) ")"))) (cdr lst))))

->  "France(1)/Germany(2)/Holland(3)/Berne(D)"
 
Title: Re: To get the string
Post by: ronjonp on August 08, 2014, 08:27:35 AM
And another to not have the trailing "/"
Code: [Select]
(vl-string-right-trim
  "/"
  (apply 'strcat
    (mapcar '(lambda (x) (strcat (last x) "(" (car x) ")/"))
       '(("1" nil "France") ("2" nil "Germany") ("3" nil "Holland") ("D" nil "Berne"))
    )
  )
)
Title: Re: To get the string
Post by: Lee Mac on August 09, 2014, 06:13:45 AM
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (if (cdr l)
  3.         (strcat (caddar l) "(" (caar l) ")/" (f (cdr l)))
  4.         (strcat (caddar l) "(" (caar l) ")")
  5.     )
  6. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (f '(("1" nil "France")("2" nil "Germany")("3" nil "Holland")("D" nil "Berne")))
  2. "France(1)/Germany(2)/Holland(3)/Berne(D)"