Author Topic: how to show alert message box with string  (Read 4659 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
how to show alert message box with string
« 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

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to show alert message box with string
« Reply #1 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)))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: how to show alert message box with string
« Reply #2 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.


TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Coder

  • Swamp Rat
  • Posts: 827
Re: how to show alert message box with string
« Reply #3 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 .

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to show alert message box with string
« Reply #4 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))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to show alert message box with string
« Reply #5 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))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Coder

  • Swamp Rat
  • Posts: 827
Re: how to show alert message box with string
« Reply #6 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 .

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: how to show alert message box with string
« Reply #7 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)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: how to show alert message box with string
« Reply #8 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))
        )
    )
)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to show alert message box with string
« Reply #9 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.
« Last Edit: October 11, 2011, 10:11:37 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

HofCAD

  • Guest
Re: how to show alert message box with string
« Reply #10 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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: how to show alert message box with string
« Reply #11 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)))
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: how to show alert message box with string
« Reply #12 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-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.