Loops
Please post comments to discussion thread
http://forums.augi.com/showthread.ph...67#post1328567
Loops are necessary in programming to process groups of data or in AutoCAD selection sets or items of lists.
Those loop functions include Foreach, Mapcar, Repeat and While. (In Visual LISP you also have Vlax-For which we will discuss later)
LISP stands for list Processing.
The first two expressions Foreach and Mapcar are for lists.
Say we make a list like this
Code:
(setq lstLetters (list "a" "b" "c" "d" "e"))
Foreach
We can iterate (process each value of the list) like this
Code:
(foreach strLetter lstLetters (print strLetter))
would print each letter out to the command line like
"a"
"b"
"c"
"d"
"e"
Mapcar
(Multiple APplications to the Contents of the Address Register)
Which means do this function to each member of the list.
Code:
(mapcar 'print lstLetters)
Would also print each letter out to the command line like
"a"
"b"
"c"
"d"
"e"
I might mention here the mysterious function Lambda.
Lambda is almost exactly the same at the well known function Defun (define function)
The only difference is Lambda doesn't have a function name and it runs immediately.
It is frequently used with the Mapcar function.
So
Code:
(mapcar '(lambda (X)(print X)) lstLetters)
This does the exact same thing as the examples above
But it allows you to perform more complicated functions like
Code:
(mapcar '(lambda (X)(print (strcat "+" X))) lstLetters)
Would print each letter out to the command line like
"+a"
"+b"
"+c"
"+d"
"+e"
It can also handle multiple lists too.
(setq lstNumerals (list "1" "2" "3" "4" "5"))
Code:
(mapcar '(lambda (X Y)(print (strcat X Y))) lstLetters lstNumerals)
Would print each letter out to the command line like
"a1"
"b2"
"c3"
"d4"
"e5"
OK enough about Mapcar.
Repeat
Repeat will repeat a section of code.
For example
Code:
(setq intCount 0)
(repeat 5
(setq intCount (1+ intCount))
(print intCount)
)
The 1+ expression adds one to the intCount variable each time it loops.
Would print each letter out to the command line like
1
2
3
4
5
The repeat expression is frequently used to read and process a selection set like this function (we will talk more about
selection set expressions later in the series.
Code:
;___________________________________________________________________________________________________________|
;
; Function to convert a selection set to a list of entities
;___________________________________________________________________________________________________________|
(defun SelectionSetToListOfEntities (ssSelections / intCount entSelection lstEntities )
(repeat (setq intCount (sslength ssSelections))
(setq intCount (1- intCount))
(setq entSelection (ssname ssSelections intCount))
(setq lstEntities (cons entSelection lstEntities))
)
lstEntities
)
While
The while expression will perform a conditional and keep looping until the condition is false for example
Code:
;___________________________________________________________________________________________________________|
;
; Function to convert a selection set to a list of entities
;___________________________________________________________________________________________________________|
(defun SelectionSetToListOfEntities (ssSelections / intCount intCountLength entSelection lstEntities )
(setq intCountLength (sslength ssSelections))
(setq intCount intCountLength)
(while (> intCount 0)
(setq intCount (1- intCount))
(setq entSelection (ssname ssSelections intCount))
(setq lstEntities (cons entSelection lstEntities))
)
lstEntities
)
Please ask me questions and ask for more examples if this is not clear for you on the discussion thread.