Author Topic: question on the use of foreach  (Read 6983 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
question on the use of foreach
« on: August 20, 2014, 04:48:00 PM »
when using foreach, do you need to use a (progn..) in there if you want to do more than 1 expression?


example would be:
Code - Auto/Visual Lisp: [Select]
  1. (foreach x lst
  2.    (progn
  3.       (do this)
  4.       (do this)
  5.       (do this)
  6.       (do this)
  7.       (do this)
  8.       (do this)
  9.     )
  10.  )


EDIT by Se7en: Fixed code tags, adjusted formatting slightly.
« Last Edit: August 21, 2014, 06:12:59 AM by John Kaul (Se7en) »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: question on the use of foreach
« Reply #1 on: August 20, 2014, 04:57:50 PM »
No.

As specified in the documentation:
Quote from: foreach documentation
The foreach function steps through a list, assigning each element in the list to a variable, and evaluates each expression for every element in the list. Any number of expressions can be specified.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: question on the use of foreach
« Reply #2 on: August 20, 2014, 07:14:18 PM »
Code - Auto/Visual Lisp: [Select]
  1. (foreach x '(1 2 3 4 5 6)
  2.   (setq lst (append lst (list(1+ x))))
  3.   )
  4.  

Will return  lst = (2 3 4 5 6 7)

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: question on the use of foreach
« Reply #3 on: August 21, 2014, 06:36:48 AM »
Off topic reminder:

...
As specified in the documentation:
Quote from: foreach documentation
The foreach function steps through a list, assigning each element in the list to a variable, and evaluates each expression for every element in the list. Any number of expressions can be specified.

As a reminder to all, TheSwamp now has it's own Autolisp help which can be linked to (to use the example above) like this:
http://www.theswamp.org/~john/avlisp/#foreach
or to remove the url:
foreach

Also note that this help file can be added to or corrected by our members. If you want a section revised, please feel free to ask me or any of the moderators.
« Last Edit: August 21, 2014, 07:23:50 AM by Kerry »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: question on the use of foreach
« Reply #4 on: August 21, 2014, 07:25:23 AM »

Not only,  but also :: if we use the cadlisp-7 code tag

Code - Auto/Visual Lisp: [Select]
  1. (foreach thingystuff
  2.     select foreach or any blue highlighted stuff with the mouse using CLICK or CTRL-CLICK and watch the magic
  3. )
  4.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: question on the use of foreach
« Reply #5 on: August 21, 2014, 08:31:14 AM »
Yes, and that too. :)

On a side note: I know it's not the prettiest webpage but I did try to offer some helpful things when creating that help file. Like, I tried to keep the HTML to a minimum so it would be as quick (fast loading) as possible and wash out the colors a little to make it more "readable" (be more like a "paper white" and "ink type black").

This place is an amazing resource and I feel that everything we can do to separate ourselves from the pack of other Autodesk related help forums the better (we should continue to be the standard those other places strive to be like).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: question on the use of foreach
« Reply #6 on: August 21, 2014, 08:52:19 AM »

Yes, you've done a great job with it John.

The file is a couple of years out of date, so it probably needs a couple of hours of TLC ... but I can't make time to do it and I'm bloody sure you won't be able to either.

Perhaps one of the newer members will volunteer :-D
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: question on the use of foreach
« Reply #7 on: August 21, 2014, 09:03:36 AM »

Yes, you've done a great job with it John.

The file is a couple of years out of date, so it probably needs a couple of hours of TLC ... but I can't make time to do it and I'm bloody sure you won't be able to either.

Perhaps one of the newer members will volunteer :-D

You are correct about that. I don't know what "free time" is anymore.

That is a wonderful idea! I hope someone volunteers.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

andrew_nao

  • Guest
Re: question on the use of foreach
« Reply #8 on: August 21, 2014, 09:06:42 AM »
hmm.. i must be using it wrong.. thanks for the info

thanks for the format correction

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: question on the use of foreach
« Reply #9 on: August 21, 2014, 09:15:26 AM »
Another option to remember ...

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: question on the use of foreach
« Reply #10 on: August 21, 2014, 10:03:07 AM »

Not only,  but also :: if we use the cadlisp-7 code tag

Code - Auto/Visual Lisp: [Select]
  1. (foreach thingystuff
  2.     select foreach or any blue highlighted stuff with the mouse using CLICK or CTRL-CLICK and watch the magic
  3. )
  4.  
Not sure why ... but those code tags have not worked for me in quite a while.  :?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: question on the use of foreach
« Reply #11 on: August 21, 2014, 10:09:26 AM »

Not only,  but also :: if we use the cadlisp-7 code tag

Code - Auto/Visual Lisp: [Select]
  1. (foreach thingystuff
  2.     select foreach or any blue highlighted stuff with the mouse using CLICK or CTRL-CLICK and watch the magic
  3. )
  4.  
Not sure why ... but those code tags have not worked for me in quite a while.  :?

Which browser ron ?
What do you mean 'don't work' > can't set or can't see what others have set ?
I wonder if it's a JavaScript setting ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: question on the use of foreach
« Reply #12 on: August 21, 2014, 10:30:10 AM »
Kerry,


I'm using Chrome. I can see when others use the tags, but they don't get added to my posts. I also get strangeness (height change \ added spaces \ font change) with formatting when using copy paste ... even paste without formatting ( ctrl+shift+v )


 :|

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: question on the use of foreach
« Reply #13 on: August 21, 2014, 10:32:18 AM »
Here is a test post using Firefox. Autolisp tags still don't work ..... hhhhmmm
No added spaces to the post though .. got some wonkiness going on.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: question on the use of foreach
« Reply #14 on: August 21, 2014, 10:43:57 AM »
test post
Code - Auto/Visual Lisp: [Select]
  1.   ;-----------------------------------------
  2.   ; returns the x y point list of a caculated midpoint
  3.   ;
  4.   ; (midpt '(0.0 0.0 0.0) '(0.0 0.0 0.0) )
  5.   ;-----------------------------------------
  6.   (defun midpt (point_1 point_2)
  7.     (mapcar
  8.       '(lambda (x y) (/ (+ x y) 2.0)) point_1 point_2
  9.       )
  10.     )
  11.  

chrome, windows 7
TheSwamp.org  (serving the CAD community since 2003)