Author Topic: question on the use of foreach  (Read 6831 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: 12905
  • 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: 10603
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: 10603
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: 10603
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: 7526
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: 7526
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: 7526
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: 28753
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)

ronjonp

  • Needs a day job
  • Posts: 7526
Re: question on the use of foreach
« Reply #15 on: August 21, 2014, 11:01:04 AM »
Test post Chrome Win 8.1

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: question on the use of foreach
« Reply #16 on: August 21, 2014, 11:06:15 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.

Weird!

The display you see (the forum/post) is just HTML with some java script for the dynamic stuff mixed in but you shouldn't have a problem viewing or typing (most forum boards aren't anything fancy or difficult for browsers to render).
For example, here is a snip of the code tag after GeSHi and SMF get done parsing the page.
Code - Text: [Select]
  1. <div class="list_posts smalltext" id="msg_526915_body">test post<br /><div class="codeheader">Code - Auto/Visual Lisp: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><pre class="cadlisp-7 geshi" style="font-family:monospace;"><ol><li style=""><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp;<span style="color: #666666; font-style: italic;">;-----------------------------------------</span></div></li><li style=""><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp;<span style="color: #666666; font-style: italic;">; returns the x y point list of a caculated midpoint</span></div></li><li style=""><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: question on the use of foreach
« Reply #17 on: August 21, 2014, 11:07:52 AM »
Test post Chrome Win 8.1

I guess you'll have to just use the code tags and add "=cadlisp-7" to the opening tag. [ code=cadlisp-7 ]
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7526
Re: question on the use of foreach
« Reply #18 on: August 21, 2014, 11:12:20 AM »
Test post Chrome Win 8.1

I guess you'll have to just use the code tags and add "=cadlisp-7" to the opening tag. [ code=cadlisp-7 ]


Yup ... I'm going to do a test post from a Win 7 machine for $h!+$  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7526
Re: question on the use of foreach
« Reply #19 on: August 21, 2014, 11:27:11 AM »
Same behavior with WinXP and Win7 maybe theme \ account related ?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: question on the use of foreach
« Reply #20 on: August 21, 2014, 11:27:49 AM »
Same behavior with WinXP and Win7 maybe theme \ account related ?

not me, win7 64 bit and it worked ok for me using chrome

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: question on the use of foreach
« Reply #21 on: August 21, 2014, 11:34:34 AM »
Same behavior with WinXP and Win7 maybe theme \ account related ?
Can you put the code tags in your post first then paste the text between them?

TheSwamp.org  (serving the CAD community since 2003)

ronjonp

  • Needs a day job
  • Posts: 7526
Re: question on the use of foreach
« Reply #22 on: August 21, 2014, 11:41:38 AM »
Code - Auto/Visual Lisp: [Select]
I can manually enter the tag, it just won't generate them.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: question on the use of foreach
« Reply #23 on: August 21, 2014, 01:12:25 PM »
Code - Auto/Visual Lisp: [Select]
I can manually enter the tag, it just won't generate them.
to weird ....
TheSwamp.org  (serving the CAD community since 2003)

ronjonp

  • Needs a day job
  • Posts: 7526
Re: question on the use of foreach
« Reply #24 on: August 21, 2014, 01:38:01 PM »
What is weird is it can be reproduced on at least 4 different computers...

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: question on the use of foreach
« Reply #25 on: August 21, 2014, 01:56:44 PM »
What is weird is it can be reproduced on at least 4 different computers...

I truly don't understand; do you live in the Bermuda triangle or some other anomalous geographic location? ...I'm at a loss. Sorry.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7526
Re: question on the use of foreach
« Reply #26 on: August 21, 2014, 02:03:08 PM »
Interesting .. just tested in IE11 *shudder* and it works fine  :? . Still no go in Firefox or Chrome.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: question on the use of foreach
« Reply #27 on: August 21, 2014, 02:04:41 PM »
Interesting .. just tested in IE11 *shudder* and it works fine  :? . Still no go in Firefox or Chrome.

lmao!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7526
Re: question on the use of foreach
« Reply #28 on: August 21, 2014, 02:13:48 PM »
Interesting .. just tested in IE11 *shudder* and it works fine  :? . Still no go in Firefox or Chrome.

lmao!


 :2funny:

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 #29 on: August 21, 2014, 06:08:08 PM »
Ron,
Do you have JavaScript enabled on the Browsers ??

I use Win 7 Pro x64 , Chrome(latest)
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: 7526
Re: question on the use of foreach
« Reply #30 on: August 21, 2014, 07:06:08 PM »
Yup.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC