Author Topic: How to create a list  (Read 1477 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 462
How to create a list
« on: August 16, 2016, 02:27:07 AM »
I am trying to create a list in the command prompt by entering a group of numbers.
What I have done is shown below. Can you help to group these numbers as a list?
In my case the numbers would be: 1st, 2nd, 3rd, 4th, 5th, and so on…
BTW obviously I will amend the code to show correctly when the numbers entered are more than 20th (i.e. 21st, 22nd, 23rd…), but I am not worried at this time.

Your helps are much appreciated.

Code: [Select]
(setq ElementInitial 1)
(while (/= ElementInitial nil)
   (if (= ElementInitial 1) (setq ListElementName "1st"))
   (if (= ElementInitial 2) (setq ListElementName "2nd"))
   (if (= ElementInitial 3) (setq ListElementName "3rd"))
   (if (> ElementInitial 3) (setq ListElementName (strcat (itoa ElementInitial) "th"))
   (setq ElementInitial (+ ElementInitial 1))
   (setq ListElement (getreal (strcat "\nEnter the " ListElementName " number: ")))
); end of while

danallen

  • Guest
Re: How to create a list
« Reply #1 on: August 16, 2016, 03:04:41 AM »
see this post for ordinal numbers

https://www.theswamp.org/index.php?topic=43830.0

mailmaverick

  • Bull Frog
  • Posts: 493
Re: How to create a list
« Reply #2 on: August 16, 2016, 03:06:59 AM »
My code :
Code: [Select]
(defun c:test ()
  (setq counter 0)
  (setq appendlist (list (cons '1 "ST") (cons '2 "ND") (cons '3 "RD") (cons '11 "TH") (cons '12 "TH") (cons '13 "TH")))
  (setq ElementList nil)
  (setq found T)
  (while found
    (setq counter (+ counter 1))
    (if (not (setq abcd (cdr (assoc counter appendlist))))
      (progn (setq tmp1 (itoa counter))
     (setq len (strlen tmp1))
     (setq tmp2 (substr tmp1 len 1))
     (setq tmp2 (atoi tmp2))
     (if (not (setq abcd (cdr (assoc tmp2 appendlist))))
       (setq abcd "TH")
     )
      )
    )
    (if (setq ListElement (getreal (strcat "\nEnter the " (itoa counter) abcd " number: ")))
      (setq ElementList (append ElementList (list ListElement)))
      (setq found nil)
    )
  )
  (princ)
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to create a list
« Reply #3 on: August 16, 2016, 08:15:40 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / n r x )
  2.     (setq n 0)
  3.     (while
  4.         (progn
  5.             (initget 4)
  6.             (setq x (getint (strcat "\nSpecify the " (itoa (setq n (1+ n))) (ordinal n) " number <done>: ")))
  7.         )
  8.         (setq r (cons x r))
  9.     )
  10.     (reverse r)
  11. )
  12.  
  13. (defun ordinal ( n )
  14.     (cond ((< 3 (rem n 100) 21) "th") ((nth (rem n 10) '(nil "st""nd""rd"))) ("th"))
  15. )

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to create a list
« Reply #4 on: August 18, 2016, 01:51:54 AM »
Thanks to Dan, mailmaverick and Lee for your helps.
And it is amazing that a couple of lines in Lee's code does all I want. - I have spent some time to learn from your code.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to create a list
« Reply #5 on: August 18, 2016, 12:45:08 PM »
You're welcome! - Feel free to ask if you have any questions about the posted code.