Author Topic: To get the string  (Read 2408 times)

0 Members and 1 Guest are viewing this topic.

2e4lite

  • Guest
To get the string
« 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)"
« Last Edit: August 07, 2014, 11:14:27 PM by 2e4lite »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is there a easy and quick way to get the string?
« Reply #1 on: August 07, 2014, 11:01:44 PM »

What code have you tried ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

2e4lite

  • Guest
Re: To get the string
« Reply #2 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 ?
« Last Edit: August 07, 2014, 11:11:22 PM by 2e4lite »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: To get the string
« Reply #3 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))
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

dgpuertas

  • Newt
  • Posts: 80
Re: To get the string
« Reply #4 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)"
 

ronjonp

  • Needs a day job
  • Posts: 7526
Re: To get the string
« Reply #5 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"))
    )
  )
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: To get the string
« Reply #6 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)"