Print a list with ALERT

Print a list with ALERT

msarqui
Collaborator Collaborator
2,024 Views
4 Replies
Message 1 of 5

Print a list with ALERT

msarqui
Collaborator
Collaborator

Hi guys,

 

May I have your help, please?

 

I can PRINT a list, separate by comma with this:

 

 

(setq a '("Red" "Blue" "Green"))
(foreach n a (princ (strcat ""n", ")) (princ))

But, how do I do the same with an ALERT, and also adding a text before and after?

 

 

(alert "\nThe colors" (foreach n a (princ (strcat ""n ", ")) (princ)) "are in the list") ;This is not working!

Thanks for your help,

Marcelo

0 Likes
Accepted solutions (2)
2,025 Views
4 Replies
Replies (4)
Message 2 of 5

phanaem
Collaborator
Collaborator
(setq a '("Red" "Blue" "Green"))
(alert
  (strcat
    "\nThe colors "
    (apply 'strcat (mapcar 'strcat '("" ", " ", ") a))
    " are in the list."
    )
  )

This will only work with a list of 3 texts.

 

Alternatively, you can use this one for any list (in fact, for any kind of data)

 

(alert
  (strcat
    "\nThe colors "
    (vl-princ-to-string a)
    " are in the list."
    )
  )
Message 3 of 5

hmsilva
Mentor
Mentor
Accepted solution

Hi Marcelo,

in addition to Stefan's great advices, we can use also something like this for a list of strings

 

(setq a '("Red" "Blue" "Green"))
(alert
  (strcat
    "\nThe colors "
    (vl-string-left-trim "," (apply 'strcat (mapcar '(lambda (x) (strcat "," x)) a)))
    " are in the list."
    )
  )

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 4 of 5

_Tharwat
Advisor
Advisor
Accepted solution

Hi,

Another way to achieve the task and it's not that different than the others;

 

(setq a '("Red" "Blue" "Green" "Yellow" "Pink"))

(alert
  (strcat
    "\nThe colors "
    (apply 'strcat (append (list (car a)) (mapcar '(lambda (x) (strcat ", " x)) (cdr a))))
    " are in the list."
    )
  )
0 Likes
Message 5 of 5

msarqui
Collaborator
Collaborator

Hi guys,

 

Thanks to all for the reply.

Both solutions from hmsilva and 
_Tharwat are exactly what I was wondering.
 
 
 
Thanks!
 
Marcelo
0 Likes