Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

math in the foreach function

6 REPLIES 6
Reply
Message 1 of 7
cadking2k5
347 Views, 6 Replies

math in the foreach function

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

6 REPLIES 6
Message 2 of 7
pbejse
in reply to: cadking2k5


@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?

 

Message 3 of 7
Kent1Cooper
in reply to: cadking2k5


@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)

)

Kent Cooper, AIA
Message 4 of 7
alanjt_
in reply to: Kent1Cooper

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))

Message 5 of 7
Kent1Cooper
in reply to: alanjt_


@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 Cooper, AIA
Message 6 of 7
alanjt_
in reply to: Kent1Cooper

@kent: I understand that, but that method is fraught with issues and I try and steer people away from it.

Message 7 of 7
pbejse
in reply to: alanjt_


@alanjt_ wrote:

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.

 


Exactly what i'm driving at. Smiley Wink

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost