TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on October 11, 2011, 07:09:56 AM

Title: how to show alert message box with string
Post 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 ..

Code: [Select]
(setq lst ("four" "three" "two" "one"))
(alert (strcat "\n you have a list of stings :" lst))

Many thanks
Title: Re: how to show alert message box with string
Post by: irneb on October 11, 2011, 07:25:19 AM
Check out the apply and strcat functions:
Code: [Select]
(alert (apply 'strcat (cons "\n you have a list of strings :" lst)))
Title: Re: how to show alert message box with string
Post by: JohnK on October 11, 2011, 07:27:36 AM
I'm sure there are other (better) ways; I just woke up.
Code: [Select]
(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.


Title: Re: how to show alert message box with string
Post by: Coder on October 11, 2011, 07:45:07 AM
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 .
Title: Re: how to show alert message box with string
Post by: irneb on October 11, 2011, 07:57:13 AM
@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:
Code: [Select]
(alert (apply 'strcat (cons "\nYou have a list of strings :" (mapcar '(lambda (s) (strcat " " s)) lst))))
Title: Re: how to show alert message box with string
Post by: irneb on October 11, 2011, 08:06:08 AM
Of course that's using normal AutoLisp. If you can use VisualLisp then this does pretty much the same thing:
Code: [Select]
(alert (vl-string-trim "()" (vl-princ-to-string (cons "\nYou have a list of strings :" lst))))
Title: Re: how to show alert message box with string
Post by: Coder on October 11, 2011, 08:21:20 AM
Of course that's using normal AutoLisp. If you can use VisualLisp then this does pretty much the same thing:
Code: [Select]
(alert (vl-string-trim "()" (vl-princ-to-string (cons "\nYou have a list of strings :" lst))))

Also very nice codes .

Thank you so much .
Title: Re: how to show alert message box with string
Post by: CAB on October 11, 2011, 09:25:17 AM
My solution.
Code: [Select]
(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)
)
Title: Re: how to show alert message box with string
Post by: Lee Mac on October 11, 2011, 09:44:42 AM
One more:

Code: [Select]
(alert
    (apply 'strcat
        (cons "You have a list of strings:"
            (apply 'append (mapcar '(lambda ( x ) (list "\n" x)) lst))
        )
    )
)
Title: Re: how to show alert message box with string
Post by: irneb on October 11, 2011, 10:06:48 AM
Or to go "really" bonkers  :lmao: :
Code: [Select]
(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:
Code: [Select]
(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.
Title: Re: how to show alert message box with string
Post by: HofCAD on October 11, 2011, 12:14:00 PM
Hello everybody .

I wonder how to show the alert message box with a list of strings as below ..

Code: [Select]
(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.
Title: Re: how to show alert message box with string
Post by: Lee Mac on October 11, 2011, 12:32:50 PM
Or to go "really" bonkers  :lmao:

You have some redundant expressions  :wink:

Code: [Select]
(defun MergeLists ( l1 l2 )
    (apply 'append (mapcar 'list l1 l2))
)
Code: [Select]
(defun GenerateList ( n item / lst )
    (repeat n (setq lst (cons item lst)))
)
Title: Re: how to show alert message box with string
Post by: CAB on October 11, 2011, 01:16:19 PM
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-)