TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Hrishikesh on July 26, 2017, 09:32:37 AM

Title: Use of multiple loops in one routine?
Post by: Hrishikesh 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
Title: Re: Use of multiple loops in one routine?
Post by: ChrisCarlson on July 26, 2017, 10:09:14 AM
In round about ways you can, the question would be why?
Title: Re: Use of multiple loops in one routine?
Post by: Hrishikesh 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...
Title: Re: Use of multiple loops in one routine?
Post by: Lee Mac 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. )
Title: Re: Use of multiple loops in one routine?
Post by: Hrishikesh on July 26, 2017, 01:28:37 PM
Thanks Lee  :-)