Jump to content

How to add items to a list?


firavolla

Recommended Posts

I'm a newbie :D (in case i didn't mention that in my earlier posts)..

So, firstly, I thank you for your help and answers and advices and so forth.

Lastly, how the crow's nest do I add items to a list?

For instance, the user has to enter a set of values, the number of values is dependant upon a number previously entered and the values are entered directly via the command prompt in acad.

So i told myself..Ok, I make a list, then use a Repeat or for command to get a new value from the user and append it to the list as a new element.

So let's say this next line is inside the repeat loop. It should be followed by a command that adds this element to a list. I tried and tried..and failed. Any help?

(setq L (getdist "\n Length: "))

Link to comment
Share on other sites

  • Replies 34
  • Created
  • Last Reply

Top Posters In This Topic

  • Se7en

    10

  • Lee Mac

    9

  • alanjt

    5

  • firavolla

    4

Top Posters In This Topic

You have many options available to you: look up CONS and APPEND in the help files.

 

Command: (setq L '(1 2 3))

(1 2 3)

 

Command: (setq L (cons 4 L))

(4 1 2 3)

 

Command: (setq L (append L '(4)))

(1 2 3 4)

 

Command: (setq L (cons L 4))

((1 2 3 4) . 4)

Link to comment
Share on other sites

(setq L (append L (list (getint "\n Lungimea: "))))

 

Something like this is obviously wrong for me...I can't find the right answer though..

Link to comment
Share on other sites

Use a conditional to check that the user has entered a value, and add it to the list:

 

Perhaps something like:

 

(while (setq item (getint "\n Number? "))
 (setq lst (cons item lst))
)

 

Lee

Link to comment
Share on other sites

WHILE is not a conditional but i understand the intension/meaning.

 

LEE, IMO the last bit of code you provided is overkill and it only obscures the intension. -i.e. This will suffice even if the list does not exist.

 

(setq lst (cons (getint "\n Number? ") lst))

 

HTH

:)

Link to comment
Share on other sites

Here's a simple one that I understand. It allows for keyboard, or two point mouse input.

 

(setq M 1)
(while (> M 0)
 (setq addL (getdist "\n Length: "))
 (if (= addL nil) (setq addL 0))
 (setq M (* M addL))
 (setq L (append (list addL) L))
)

 

Chuck

Link to comment
Share on other sites

Here's a simple one that I understand. It allows for keyboard, or two point mouse input.

 

(setq M 1)
(while (> M 0)
 (setq addL (getdist "\n Length: "))
 (if (= addL nil) (setq addL 0))
 (setq M (* M addL))
 (setq L (append (list addL) L))
)

Chuck

 

(defun foo (/ d lst)
 (while (setq d (getdist "\nSpecify length: "))
   (setq lst (cons d lst))
 )
)

Link to comment
Share on other sites

I knew one of these smart guys could shorten it. I trudge on through my LISP, but am learning the better, more efficient methods thanks to this forum.

 

Chuck

Link to comment
Share on other sites

I knew one of these smart guys could shorten it. I trudge on through my LISP, but am learning the better, more efficient methods thanks to this forum.

 

Chuck

:)

 

Just in case you were trying to accomplish with only points...

 

(defun foo (/ pt lst)
 (if (car (setq lst (list (getpoint "\nSpeicfy point: "))))
   ((lambda (dist)
      (while (setq pt (getpoint (car lst) "\nSpecify next point: "))
        (grdraw (cadr (setq lst (cons pt lst))) pt 1 -1)
        (setq dist (+ (distance pt (cadr lst)) dist))
      )
    )
     0.
   )
 )
)

Link to comment
Share on other sites

WHILE is not a conditional but i understand the intension/meaning.

 

yeah it is - conditional loop.

 

LEE, IMO the last bit of code you provided is overkill and it only obscures the intension[sic]. -i.e. This will suffice even if the list does not exist.

 

(setq lst (cons (getint "\n Number? ") lst))

 

I would disagree, I realise that would suffice if the list did not exist, but the OP mentions including it in a loop, so you don't really want a list looking like this...

 

'( 1 2 nil 3 1 nil 2)

 

do ya...

Link to comment
Share on other sites

A few from my library:

 

(defun LM:GetPoints ( / lst pt )
 ;; Lee Mac  ~  05.03.10
 ;; Returns a list of selected Points
 
 (if (car (setq lst (list (getpoint "\nPick First Point: "))))
   
   (while (setq pt (getpoint "\nPick Next Point: " (car lst)))
     (mapcar
       (function
         (lambda ( from to ) (grdraw from to 3 1))
       )
       (cdr (reverse (setq lst (cons pt lst))))

       (reverse (cdr lst))
     )
   )
 )
 (redraw) (reverse lst)
)


(defun LM:GetWindow ( / lst pt )
 ;; Lee Mac  ~  05.03.10
 ;; Returns a list of Points defining the window.
 
 (if (car (setq lst (list (getpoint "\nPick First Point: "))))
   
   (while (setq pt (getpoint "\nPick Next Point: " (car lst))) (redraw)
     (mapcar
       (function
         (lambda ( from to ) (grdraw from to 3 1))
       )
       (append lst (list pt)) (setq lst (cons pt lst))
     )
   )
 )
 (redraw) (reverse lst)
)


(defun LM:GetDynWindow ( / lst pt gr )
 ;; Lee Mac  ~  31.01.10

 (if (and (car (setq lst (list (getpoint "\nPick First Point: "))))
          (princ "\nPick Next Point: "))
   (while
     (setq pt
       (progn
         (while (and (= 5 (car (setq gr (grread 't 5 0)))) (listp (cadr gr))) (redraw)
           (mapcar
             (function
               (lambda ( from to ) (grdraw from to 3 1))
             )
             (cons (cadr gr) lst) (append lst (cdr gr))
           )
         )
         (cond ( (listp (cadr gr)) (cadr gr) ))
       )
     )
     (cond ( pt (setq lst (cons pt lst))))
   )
 )
 (redraw) (reverse lst)
)

 

Link to comment
Share on other sites

`conditional loop'? Where did you hear that one?

 

Ok. Good 'nuff for me, that method is prolly better for the OP anyways.

 

 

 

Whats wrong with lists like that? I have lists like that all the time. :P

Link to comment
Share on other sites

Had a meeting and couldn't post it earlier...

(defun AT:DistFromPoints (2D? / pt lst)
 ;; Return total distance between picked points
 ;; 2D? - returned distance is 3D or 2D (T for 2D, nil for 3D)
 ;; Alan J. Thompson, 06.17.10
 (if (car (setq lst (list (getpoint "\nSpeicfy point: "))))
   ((lambda (dist)
      (while (setq pt (getpoint (car lst) "\nSpecify next point: "))
        (grdraw (cadr (setq lst (cons pt lst))) pt 1 -1)
        (setq dist (+ (if 2D?
                        (distance (list (car pt) (cadr pt)) (list (caadr lst) (cadadr lst)))
                        (distance pt (cadr lst))
                      )
                      dist
                   )
        )
      )
    )
     0.
   )
 )
)

Link to comment
Share on other sites

Yeah, keep your skirt on Nancy....Well, you call it what you want but I'm still not gonna call a WHILE a `conditional loop'.

 

BTW, ive been trying to follow links on those pages for the last 20 min *argh!* and ive determined that i dont like wikipedia.

 

 

:P

Link to comment
Share on other sites

This thread has been fun to watch, conditionally speaking. :lol:

 

As a bonus, I get to see your thought process while writing code Alan.:shifty:

Link to comment
Share on other sites

BTW, ive been trying to follow links on those pages for the last 20 min *argh!* and ive determined that i dont like wikipedia.

 

 

:P

It's Wikipedia, just change it to fit your liking. :lol:

 

 

As a bonus, I get to see your thought process while writing code Alan.:shifty:

Dear god, I feel sorry for the poor soul that watches my crazy thought process. :wacko:

Link to comment
Share on other sites

Ok, so...first of all thank you for your replies.

Secondly, while is indeed a conditional loop.

Lastly, It still doesn't work. Even if it's a good idea to check user input for valid data, in my case, for now, it was me entering the data and it was obviously correct.

For instance, let's say I run the routine. I get to the 'repeat' part, I am asked to enter the first value, i enter something like 3 and then it asks me for the second value, i enter 4 and bleaaah!, error. The succession is something like

nrOp: 4 //the counter

L: 3 //first element

L: 4 //second element

; error: bad argument type: listp 3 //bleeaaah!

:cry:

 

and the routine goes like this (at least this part)

(repeat nrOp

(setq h (getint "\n Lungimea: "))

(setq L (append L '(h)))

 

)

 

So?? A simple solution?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...