Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
cadking2k5
Posts: 71
Registered: ‎04-17-2012
Message 1 of 7 (203 Views)

math in the foreach function

203 Views, 6 Replies
05-15-2013 07:18 PM

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

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 2 of 7 (196 Views)

Re: math in the foreach function

05-15-2013 09:04 PM 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?

 

*Expert Elite*
Kent1Cooper
Posts: 5,237
Registered: ‎09-13-2004
Message 3 of 7 (161 Views)

Re: math in the foreach function

05-16-2013 05:47 AM 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
Valued Mentor
alanjt_
Posts: 469
Registered: ‎08-25-2008
Message 4 of 7 (156 Views)

Re: math in the foreach function

05-16-2013 06:06 AM 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))

*Expert Elite*
Kent1Cooper
Posts: 5,237
Registered: ‎09-13-2004
Message 5 of 7 (147 Views)

Re: math in the foreach function

05-16-2013 06:38 AM 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
Valued Mentor
alanjt_
Posts: 469
Registered: ‎08-25-2008
Message 6 of 7 (144 Views)

Re: math in the foreach function

05-16-2013 06:49 AM in reply to: Kent1Cooper

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

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 7 of 7 (113 Views)

Re: math in the foreach function

05-16-2013 08:25 PM 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. :smileywink:

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community