in the foreach function can you use math like (foreach pt1 (add 1) so that foreach one you pick it adds one the next one will be named pt2 then pt3 and so on
@cadking2k5 wrote:in the foreach function can you use math like (foreach pt1 (add 1) so that foreach one you pick it adds one the next one will be named pt2 then pt3 and so on
Depending on the usage there might be a better way to do that. what exactly are you after?
@cadking2k5 wrote:in the foreach function can you use math like (foreach pt1 (add 1) so that foreach one you pick it adds one the next one will be named pt2 then pt3 and so on
I think something like (while) is more what you're after. The (foreach) function requires a collection of things in a list that it will step through, but it sounds like you want to build a collection of things not in a list, but with their own separate variable names. This would be a good application for the (set) function, since unlike (setq), it can use other functions to make the variable name. If you're asking the User to select point locations:
(setq inc 0); incrementing variable
(while
(setq pt (getpoint (strcat "\nSelect point for variable pt" (itoa (setq inc (1+ inc))) ": ")))
(set (read (strcat "pt" (itoa inc))) pt)
)
I wouldn't fool with assigning a varaible to an unknown number of points. Just add them to a list, assigned to one variable and extract with car/cadr/caddr/etc or nth, or step through the list with foreach/mapcar.
eg.
(defun _getPoints (/ lst pt) (if (car (setq lst (list (getpoint "\nSpecify first point: ")))) (while (setq pt (getpoint (car lst) "\nSpecify next point: ")) (setq lst (cons pt lst)) ) ) (reverse lst) )
(setq points (_getpoints))
@alanjt_ wrote:I wouldn't fool with assigning a varaible to an unknown number of points. Just add them to a list....
I agree that, at least for many possible uses, that can be a better approach. [I was just showing one way to do specifically what they asked to do.]
@kent: I understand that, but that method is fraught with issues and I try and steer people away from it.