TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on October 11, 2011, 07:09:56 AM
-
Hello everybody .
I wonder how to show the alert message box with a list of strings as below ..
(setq lst ("four" "three" "two" "one"))
(alert (strcat "\n you have a list of stings :" lst))
Many thanks
-
Check out the apply and strcat functions:
(alert (apply 'strcat (cons "\n you have a list of strings :" lst)))
-
I'm sure there are other (better) ways; I just woke up.
(alert
(strcat "\nYou have a list of strings : "
(apply 'strcat
(mapcar
'(lambda ( str )
(strcat str " ")) lst))))
**EDIT:
Ah, see, irneb already beat me too it.
-
Very nice irneb and seven .
the codes by seven is allowing me to have a space and I added a comma between each string in the list to have a good looking in the alert message . :lol:
Thank you so much .
-
@Se7en: No worries, I've been up for 8 hours already ... so it wasn't "fair" ;)
Anyhow, it's debatable if mine is "better" ... it's just shorter and only calls strcat once, though it calls cons where yours doesn't. What yours is doing though is adding spaces in front of all strings through the mapcar ... didn't know if the OP wanted this?
@coder: That's correct, mine simply concatenates the list together as is. To "incorporate" the adding of spaces to my code:
(alert (apply 'strcat (cons "\nYou have a list of strings :" (mapcar '(lambda (s) (strcat " " s)) lst))))
-
Of course that's using normal AutoLisp. If you can use VisualLisp then this does pretty much the same thing:
(alert (vl-string-trim "()" (vl-princ-to-string (cons "\nYou have a list of strings :" lst))))
-
Of course that's using normal AutoLisp. If you can use VisualLisp then this does pretty much the same thing:
(alert (vl-string-trim "()" (vl-princ-to-string (cons "\nYou have a list of strings :" lst))))
Also very nice codes .
Thank you so much .
-
My solution.
(defun c:test(/ lst)
(defun AddComma(lst / str)
(mapcar '(lambda (x) (setq str (if str (strcat str "," x) x))) lst)
str
)
(setq lst (AddComma '("four" "three" "two" "one")))
(alert (strcat "\n you have a list of stings : " lst))
(princ)
)
-
One more:
(alert
(apply 'strcat
(cons "You have a list of strings:"
(apply 'append (mapcar '(lambda ( x ) (list "\n" x)) lst))
)
)
)
-
Or to go "really" bonkers :lmao: :
(defun MergeLists (l1 l2 / l)
(apply 'append (mapcar '(lambda (i1 i2) (list i1 i2)) l1 l2))
)
(defun GenerateList (n item / lst)
(while (> n 0)
(setq lst (cons item lst) n (1- n))
)
lst
)
(defun StrCatLstMerge (l1 l2 / )
(apply 'strcat (MergeLists l1 l2))
)
(defun StrCatLstDelim (del lst / )
(StrCatLstMerge (GenerateList (length lst) del) lst)
)
Then you could run the following:(alert (StrCatLstDelim "\n" (cons "You have a list of strings :" lst)))
Edit: BTW Cab, why use the mapcar? The way you're using it a foreach would work just as well and not need a lambda.
-
Hello everybody .
I wonder how to show the alert message box with a list of strings as below ..
(setq lst ("four" "three" "two" "one"))
(alert (strcat "\n you have a list of stings :" lst))
Many thanks
See: http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Custom-dcl/td-p/1631074
Regards HofCAD CSI.
-
Or to go "really" bonkers :lmao:
You have some redundant expressions :wink:
(defun MergeLists ( l1 l2 )
(apply 'append (mapcar 'list l1 l2))
)
(defun GenerateList ( n item / lst )
(repeat n (setq lst (cons item lst)))
)
-
Edit: BTW Cab, why use the mapcar? The way you're using it a foreach would work just as well and not need a lambda.
Written so many years ago, pulled from my library, I could not remember my thinking at the time. 8-)