firavolla Posted June 17, 2010 Share Posted June 17, 2010 I'm a newbie (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: ")) Quote Link to comment Share on other sites More sharing options...
Se7en Posted June 17, 2010 Share Posted June 17, 2010 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) Quote Link to comment Share on other sites More sharing options...
firavolla Posted June 17, 2010 Author Share Posted June 17, 2010 (setq L (append L (list (getint "\n Lungimea: ")))) Something like this is obviously wrong for me...I can't find the right answer though.. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 17, 2010 Share Posted June 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
Se7en Posted June 17, 2010 Share Posted June 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
CHLUCFENG Posted June 17, 2010 Share Posted June 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
alanjt Posted June 17, 2010 Share Posted June 17, 2010 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)) ) ) Quote Link to comment Share on other sites More sharing options...
CHLUCFENG Posted June 17, 2010 Share Posted June 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
alanjt Posted June 17, 2010 Share Posted June 17, 2010 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. ) ) ) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 17, 2010 Share Posted June 17, 2010 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... Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 17, 2010 Share Posted June 17, 2010 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) ) Quote Link to comment Share on other sites More sharing options...
Se7en Posted June 17, 2010 Share Posted June 17, 2010 `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. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 17, 2010 Share Posted June 17, 2010 `conditional loop'? Where did you hear that one? Common knowledge dude Quote Link to comment Share on other sites More sharing options...
Se7en Posted June 17, 2010 Share Posted June 17, 2010 huh?! Now that's just weird. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 17, 2010 Share Posted June 17, 2010 Still a conditional loop lol Quote Link to comment Share on other sites More sharing options...
alanjt Posted June 17, 2010 Share Posted June 17, 2010 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. ) ) ) Quote Link to comment Share on other sites More sharing options...
Se7en Posted June 17, 2010 Share Posted June 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
CHLUCFENG Posted June 17, 2010 Share Posted June 17, 2010 This thread has been fun to watch, conditionally speaking. As a bonus, I get to see your thought process while writing code Alan. Quote Link to comment Share on other sites More sharing options...
alanjt Posted June 17, 2010 Share Posted June 17, 2010 BTW, ive been trying to follow links on those pages for the last 20 min *argh!* and ive determined that i dont like wikipedia. It's Wikipedia, just change it to fit your liking. As a bonus, I get to see your thought process while writing code Alan. Dear god, I feel sorry for the poor soul that watches my crazy thought process. Quote Link to comment Share on other sites More sharing options...
firavolla Posted June 18, 2010 Author Share Posted June 18, 2010 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! 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.