Author Topic: Use of multiple loops in one routine?  (Read 1345 times)

0 Members and 1 Guest are viewing this topic.

Hrishikesh

  • Guest
Use of multiple loops in one routine?
« on: July 26, 2017, 09:32:37 AM »
Hi,
Is it possible to use one loop multiple times in same loop? Or more than one loop under another loop?
example
1.
(repeat
(repeat
..........
..........
)
)

2.
(foreach
(foreach
(foreach
.............
.............
.............
)
)
)

3.
(repeat
(foreach
.............
.............
)
)

Thanks,
Hrishikesh

ChrisCarlson

  • Guest
Re: Use of multiple loops in one routine?
« Reply #1 on: July 26, 2017, 10:09:14 AM »
In round about ways you can, the question would be why?

Hrishikesh

  • Guest
Re: Use of multiple loops in one routine?
« Reply #2 on: July 26, 2017, 12:33:58 PM »
the question would be why?
Today I am doing some Automation in Excel, looping some functions in one formula to obtain results.
I know Excel & Autolisp has different platforms for different purposes but I am just curious that can it be possible in autolisp & how?
Becase excel vba or excel itself has different syntex than autolisp.
Just a curiosity...

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Use of multiple loops in one routine?
« Reply #3 on: July 26, 2017, 12:45:35 PM »
Certainly possible: since the expression argument for each of these looping functions is arbitrary, it may of course itself be an expression evaluating another looping function, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (foreach x '("a" "b" "c" "d") (foreach y '(1 2 3) (print x) (princ y)))
  2.  
  3. "a" 1
  4. "a" 2
  5. "a" 3
  6. "b" 1
  7. "b" 2
  8. "b" 3
  9. "c" 1
  10. "c" 2
  11. "c" 3
  12. "d" 1
  13. "d" 2
  14. "d" 3

As a more practical example, consider the following program which uses a nested repeat expression to iterate over every cell in a selected AutoCAD table:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / col obj row sel )
  2.     (if (setq sel (ssget "_+.:E:S:L" '((0 . "ACAD_TABLE"))))
  3.         (progn
  4.             (setq obj (vlax-ename->vla-object (ssname sel 0)))
  5.             (repeat (setq col (vla-get-columns obj))
  6.                 (repeat (setq col (1- col) row (vla-get-rows obj))
  7.                     (vla-settext obj (setq row (1- row)) col (strcat "R" (itoa row) "C" (itoa col)))
  8.                 )
  9.             )
  10.         )
  11.     )
  12.     (princ)
  13. )

Hrishikesh

  • Guest
Re: Use of multiple loops in one routine?
« Reply #4 on: July 26, 2017, 01:28:37 PM »
Thanks Lee  :-)